Re: Simple integer math benchmark
- Posted by ed_davis Dec 07, 2013
- 5909 views
Not wanting to short-change anyone, here are a couple of updates:
Lua and Ruby are _much_ faster if I make a couple of simple changes - no change in the actual algorithm (that would be cheating ).
My original Lua version was:
pc = 0 n = 1 lim = 5000000 while n < lim do k = 3 p = 1 n = n + 2 while k * k <= n and p do p = math.floor(n / k) * k ~= n k = k + 2 end if p then pc = pc + 1 end end print(pc)I changed it to:
local pc = 0 local n = 1 local lim = 5000000 while n < lim do local k = 3 local p = 1 ... The rest is the same.
And the Lua time goes from 105.56 seconds down to 38.16 seconds.
My original Ruby version was:
include Math pc = 0 n = 1 lim = 5000000 while n < lim do k = 3 p = 1 n = n + 2 while k * k <= n and p do p = (n / k).floor * k != n k = k + 2 end if p then pc = pc + 1 end end puts pcI changed this line:
p = (n / k).floor * k != nto:
p = n.to_i / k.to_i * k != nAnd the Ruby time goes from 242.52 down to 43.38 seconds.
But Euphoria still beats them by a good margin.