Re: Simple integer math benchmark

new topic     » goto parent     » topic index » view thread      » older message » newer message

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 smile ).

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 pc 
I changed this line:
    p = (n / k).floor * k != n 
to:
    p = n.to_i / k.to_i * k != n 
And the Ruby time goes from 242.52 down to 43.38 seconds.

But Euphoria still beats them by a good margin. smile

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu