- Sep 2, 2013I added the following lines to see what was happening at the token level, and the results are non-conclusive.

print "Using in-line calculation, with optimization method 1 (indv variables), "; now =time$( "milliseconds")

for i =1 to 100000 i2 = i * 2 : i3 = i * 3 : i4 = i * 4 result =i2 * i2 + i3 * i3 + i4 * i4 next i

print time$( "milliseconds") -now, " milliseconds"

print "Using in-line calculation, with optimization - method 2 (addition) , "; now =time$( "milliseconds")

for i =1 to 100000 i2 = i + i : i3 = i2 + i : i4 = i3 + i result =i2 * i2 + i3 * i3 + i4 * i4 next i

print time$( "milliseconds") -now, " milliseconds" wait

Using LB runtime tokenised I got :

Using exponentiation /power function it took 2726 milliseconds Using multiplication /product function it took 1929 milliseconds Using in-line calculation, no function, product rather than exponentiation, 830 milliseconds Using in-line calculation, with optimization method 1 (indv variables), 737 milliseconds Using in-line calculation, with optimization - method 2 (addition) , 740 milliseconds

Using LB Booster I got :

Using exponentiation /power function it took 227 milliseconds Using multiplication /product function it took 261 milliseconds Using in-line calculation, no function, product rather than exponentiation, 103 milliseconds Using in-line calculation, with optimization method 1 (indv variables), 210 milliseconds Using in-line calculation, with optimization - method 2 (addition) , 245 milliseconds

Which, apart from what I knew already about the compilation and speed generally, rather shows that the compiler in LB Booster treats function calls and math rather differently that the token parser in LB proper.

Interesting and pleasing, since I have a habit of writing lots of functions / subs to do anything that I need to do more than once....

Ken

--- In libertybasic@yahoogroups.com, <libertybasic@yahoogroups.com> wrote:

Gentlemen,

Interesting race.

In my computer, here are the reults in millisec:

Using direct calculation: 687

Using pruduct function: 1497

Using call/sub: 1607

Using power function: 1638

Using gosub: 2418

The reason why Usain Bolt clocked at 687 ms, is that he calculates while running. The others go somewhere to calculate and then return to continue the run.

--- In libertybasic@yahoogroups.com , < libertybasic@yahoogroups.com > wrote:

Of course!

It's worth anyone who has not realised this to try the following code

which shows the gain.

However, pedagogically ( ie wearing my ex-schoolmaster hat) I've never

met anyone who quotes Pythagoras starting 'The hypotenuse multiplied by

itself is equal to ...', or says 'the power dissipated in a resistor R

carrying I amps is I I R'.

And as always, if speed is important why use a function anyway?

'CODE __________________________________________________

print "Using exponentiation /power function it took ";

now =time$( "milliseconds")

for i =1 to 100000

result =VS( 2 *i, 3 *i, 4 *i)

next i

print time$( "milliseconds") -now, " milliseconds"

print "Using multiplication /product function it took ";

now =time$( "milliseconds")

for i =1 to 100000

result =VS2( 2 *i, 3 *i, 4 *i)

next i

print time$( "milliseconds") -now, " milliseconds"

print "Using in-line calculation, no function, product rather than

exponentiation, ";

now =time$( "milliseconds")

for i =1 to 100000

result =2 *i *2 *i+3 *i *3 *i +4 *i *4 *i

next i

print time$( "milliseconds") -now, " milliseconds"

end

' _____________________________________________________

function VS( x, y, z) ' here we define the function

VS =x^2 +y^2 +z^2

end function

function VS2( x, y, z) ' here we define the function

VS =x*x +y*y +z*z

end function - << Previous post in topic Next post in topic >>