Sorry, an error occurred while loading the content.

## 46978RE: Re: [libertybasic] help regarding old basic command

Expand Messages
• Sep 2, 2013
• 0 Attachment
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
• Show all 8 messages in this topic