Optimization

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

Hello all,
While doing some simple benchmarks in Euphoria, I came across
a very interesting thing. I ran a program like this:

-- code begins here

procedure proc1()
    atom foo
    foo=0

    for temp=1 to 15000000 do
        foo=foo+1
        foo=foo+1
        foo=foo+1
        foo=foo+1
    end for

    ? foo
end procedure


procedure proc2()
    atom foo
    foo=0

    for temp=1 to 15000000 do
        foo=foo+1
        foo=foo+1
    end for

    for temp=1 to 15000000 do
        foo=foo+1
        foo=foo+1
    end for

    ? foo
end procedure


procedure proc3()
    atom foo
    foo=0

    for temp=1 to 15000000 do
        foo=foo+1
    end for

    for temp=1 to 15000000 do
        foo=foo+1
    end for

    for temp=1 to 15000000 do
        foo=foo+1
    end for

    for temp=1 to 15000000 do
        foo=foo+1
    end for

    ? foo

end procedure
atom start

-- Time proc1()
start=time()
proc1()
printf(1,"proc1() took %2.2f seconds\n\n",time()-start)

-- Time proc2()
start=time()
proc2()
printf(1,"proc2() took %2.2f seconds\n\n",time()-start)

-- Time proc3()
start=time()
proc3()
printf(1,"proc3() took %2.2f seconds\n\n",time()-start)

-- code ends here

The three procedures do basically the same thing, but proc1()
has only one for loop, while proc2() has two of them and
proc3() has four for loops. So I supposed proc2() would be
slower than proc1() and proc3() would be slower than proc2().
However, proc1() took about 6 seconds to run and proc2() took
about 7.8 seconds (all right until now), but proc3() took
only about 5.3 seconds!

The only explanation I could find is that there is some kind
of optimization for for loops with a single statement inside.
Maybe someone can tell me if it is right or wrong. I just
wanted to send this because if I am right some programs may
be optimized to become a few milliseconds faster.

Regards,
Davi T. Figueiredo
davitf at usa.net

____________________________________________________________________
Get free e-mail and a permanent address at http://www.amexmail.com/?A=1

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

Search



Quick Links

User menu

Not signed in.

Misc Menu