Strange Times

BlitzMax Forums/BlitzMax Programming/Strange Times

Perturbatio(Posted 2006) [#1]
Running the following code takes ~1760 milliseconds to complete

SuperStrict
Delay 1000
Local starttime:Int
starttime = MilliSecs()

Local num:Int = 0
Local loopMax:Int = length()-1


For Local i:Int = 0 To loopMax
	num:+ 1
Next
Print num
Print MilliSecs()-starttime


Function length:Int()
	Return 1000000000
End Function


removing the -1 from the declaration of loopMax drops it by about 500 ms, regardless of whether I place the starttime initialisation before or after this line.

why?


Zethrax(Posted 2006) [#2]
The times I got only varied by around a millisecond. My first attempt was 40 ms longer for both tries than subsequent attempts, though, probably due to caching.


H&K(Posted 2006) [#3]
Im getting the same gap as Pert, and its wierd, cos I thought "InLine", but if you swap the 1000000000 for the var loopmax, you get the longer result.

I have the same Athlon64 3000 as pert, what do you have bill?

SuperStrict
Delay 1000
Local starttime:Int
starttime = MilliSecs()


Local num:Int = 0


For Local i:Int = 0 To 999999999
	num:+ 1
Next
Print num
Print MilliSecs()-starttime
Gives the longer result, but the first code with the -1 removed gives ~500 faster

It has to be that starttime is being corrupted, but cannot see how


Perturbatio(Posted 2006) [#4]
Good, I'm not going crazy :)


Curtastic(Posted 2006) [#5]
Happens for me too. One just happens to generate assembly code that is faster than the other right?


Perturbatio(Posted 2006) [#6]
One just happens to generate assembly code that is faster than the other right?


Apparently so, but why would that affect the loop time?

The loop uses the same variable in either one and placing the starttime portion after the loopMax declaration does nothing to speed it up. Surely a for loop is generated in the same way regardless of how the variable is assigned it's value?


TomToad(Posted 2006) [#7]
try this

When doing timing tests, you should declare variables ahead of time and allow a delay for variable setup to occur before starting timer. In your case, the operation of subtracting 1 from length() and the overhead of assigning the result to loopMax caused the extra delay which you didn't get without the subtraction. By declaring the variables first and the Delay 1000 to allow overhead, that will allow the program to time only the loop itself and the times are more to what's expected.


Perturbatio(Posted 2006) [#8]
nope, still does it. As I said above, regardless of whether I initialize my starttime before or after the declaration of loopMax.


H&K(Posted 2006) [#9]
I spent quite a long time last night puttint starttime2s and 3s everywhere. And It has to be starttime being corrupted. It cannot really be simply that its faster.

But anyway, Its somthing I cannot stop from happening if it really happens, so Im going to just wait until someone tells me why.


Grey Alien(Posted 2006) [#10]
the same on my PC with/without the -1 in non-debug mode = 480ms.


Perturbatio(Posted 2006) [#11]
strangely my Dad's machine doesn't do it (or at least the difference is minimal (less than 10ms), he has an Athlon 3200


dmaz(Posted 2006) [#12]
maybe a register thing...?
I get the same results if I subtract 1 from millisecs "starttime = MilliSecs()-1" instead of "length()".

If I declare and initialize the var ahead of the timing I get consistant results with or without the "-1" but they are now 50% slower than the fastest result but 500 ms faster than perts code on my machine.


pert's original except prints moved out of timing
SuperStrict
Delay 1000
Local starttime:Int

starttime = MilliSecs()
Local num:Int = 0
Local loopMax:Int = length()-1

For Local i:Int = 0 To loopMax
	num:+ 1
Next
starttime = MilliSecs()-starttime

Print num
Print starttime

Function length:Int()
	Return 1000000000
End Function
I avg 1900

no subtraction
SuperStrict
Delay 1000
Local starttime:Int

starttime = MilliSecs()
Local num:Int = 0
Local loopMax:Int = length()

For Local i:Int = 0 To loopMax
	num:+ 1
Next
starttime = MilliSecs()-starttime

Print num
Print starttime

Function length:Int()
	Return 1000000000
End Function
I avg 956

declare ahead of time
SuperStrict
Delay 1000
Local starttime:Int

Local num:Int = 0
Local loopMax:Int = length()

starttime = MilliSecs()
For Local i:Int = 0 To loopMax
	num:+ 1
Next
starttime = MilliSecs()-starttime

Print num
Print starttime

Function length:Int()
	Return 1000000000
End Function
I avg 1435

all of these were run in release.


dmaz(Posted 2006) [#13]
the problem doesn't happen if you multiply or divide by 1 but adding or subtracting 1 will see the delay.


Grey Alien(Posted 2006) [#14]
Do you think this could be an Athlon vs Intel thing? I have a P4 3.2GHz HT.


H&K(Posted 2006) [#15]
I would also like to know, why Greys P4 3.2 is three times faster than my Athlon64 3000
Infact why are all of dmaz's figures faster than mine by about 25%

Ive just checked and Ive got a 3200+, which is more than I thought I had, and so still begs the question why is mine so slow


Perturbatio(Posted 2006) [#16]
http://www.blitzbasic.com/Community/posts.php?topic=63966


xlsior(Posted 2006) [#17]
I'm seeing:

1807 using the length()-1
1555 using just length()

AMD Athlon 2800+


REDi(Posted 2006) [#18]
I get ~1900 with the -1, and ~1300 without, but with this...
SuperStrict
Delay 1000
Local starttime:Int
starttime = MilliSecs()

Local num:Int = 0
Local loopMax:Int = length()-1

Local i:Int
For i = 0 To loopMax
	num:+ 1
Next
Print num
Print MilliSecs()-starttime


Function length:Double()
	Return 1000000000
End Function
I get ~1200, whether the -1 is in or not. ;)

EDIT - It seems Long suffers the same as Int.


Grey Alien(Posted 2006) [#19]
see you've all got athlons, and I guess "Bad cache boundaries" ;-)


SpaceAce(Posted 2006) [#20]
Running all three of dmaz's versions, I consistently get ~1300, results being within a handful of milliseconds of one another. I have an Athlon 64 X2 Dual Core 4600+ with two gigs of cheapo RAM.

Edit: Interestingly, changing the target from 1,000,000,000 to 10,000,000,000 only adds about 40% to the execution time.

Edit again: Nevermind, it's INT, I didn't notice that.

SpaceAce


Leiden(Posted 2006) [#21]
Runnin the original code posted by Pert I get 1580. Athlon64 3200+ 939.


H&K(Posted 2006) [#22]
@Leiden, you have to remove the -1 and post the two different times. The thread is about the difference between thesse two


Jesse(Posted 2006) [#23]
now try it like this:
SuperStrict
Delay 1000
Local starttime:Int
starttime = MilliSecs()

Local num:Int = 0
Local loopMax:Int = length()-1#


For Local i:Int = 0 To loopMax
	num:+ 1
Next
Print num
Print MilliSecs()-starttime


Function length:Int()
	Return 1000000000
End Function


I get:
1057 with out subtractiong
1671 with -1
1057 with -1#
????????????


H&K(Posted 2006) [#24]
1287 -1#
1934
1290 -1