Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Speed
#1
I was doing some testing earlier and came across some REALLY strange results. This is the code:

Code:
dim t as double
t = timer
'Manhattan distance
for x = 0 to 1000000000
   a = abs(100-32)+abs(432-123)
next
print timer - t
t = timer
'"quick" distance
for x = 0 to 1000000000
   a = (100-32)*(100-32)+(432-123)*(432-123)
next
print timer - t
t = timer
'real distance
for x = 0 to 1000000000
   a = SQR((100-32)*(100-32)+(432-123)*(432-123))
next
print timer - t
sleep

The Manhattan distance was the slowest, I kind of expected this. But the real distance was actually faster than the "quick distance". Real distance does an extra command, why is it going faster?

My output:

13.29417....
13.25091....
11.52964....
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#2
hello wallace, (sorry bad english)
this is not an real benchmark if you write (100-50) FreeBASIC will replace this const values as 50 and will never use math in runtime.

And two calls to ABS(const) can be slower than one call to SQR(const)
but not on my Linux box.
Code:
2.9163973583196
2.67481268542869
2.97390921738549


You know what i mean?

Joshy
sorry about my english
Reply
#3
Take a look if you don't use const's.
Code:
3.27473725674259
5.19063343334603
18.4543648066511
Code:
option explicit
dim as double t,a,v100,v32,v123,v432
dim as integer x
v100=100:v32=32:v123=123:v432=432
t = timer
'Manhattan distance
for x = 0 to 1000000000
   a = abs(v100-v32)+abs(v432-v123)
next
print timer - t
t = timer
'"quick" distance
for x = 0 to 1000000000
   a = (v100-v32)*(v100-v32)+(v432-v123)*(v432-v123)
next
print timer - t
t = timer
'real distance
for x = 0 to 1000000000
   a = SQR((v100-v32)*(v100-v32)+(v432-v123)*(v432-v123))
next
print timer - t
sleep
sorry about my english
Reply
#4
You should also consider doing more than one test, since there are many factors at program start that can skew results (On my machine, Manhattan and Quick go back and forth, with an edge going to Manhattan):

Code:
option explicit
option byval

function Manhattan( x1 as integer, y1 as integer, x2 as integer, y2 as integer ) as double
    return abs(x2-x1) + abs(y2-y1)
end function

function Quick( x1 as integer, y1 as integer, x2 as integer, y2 as integer ) as double
    return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)
end function

function Real( x1 as integer, y1 as integer, x2 as integer, y2 as integer ) as double
    return Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
end function

type as function( x1 as integer, y1 as integer, x2 as integer, y2 as integer ) as double DistanceFunction

function DistanceFunctionTest( f as DistanceFunction, n as integer ) as double
    dim as integer x1 => 32, y1 => 123
    dim as integer x2 => 100, y2 => 432

    dim as double testStart => Timer()
    while( n > 0 )
        dim as double d => f( x1,y1,x2,y2 )
        n -= 1
    wend
    return Timer() - testStart
end function

    const numFunctionCalls as integer = 100000000
    const numTests as integer = 10

    dim as integer currentTest => 0
    while( currentTest < numTests )
        Print "Test #" ; currentTest+1 ; ":"
        Print " Manhattan total time: " ; DistanceFunctionTest( @Manhattan, numFunctionCalls )
        Print " Quick total time: " ; DistanceFunctionTest( @Quick, numFunctionCalls )
        Print " Real total time: " ; DistanceFunctionTest( @Real, numFunctionCalls )
        currentTest += 1
    wend

    sleep : end 0
stylin:
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)