don't bother storing calculated array index in var

BlitzMax Forums/BlitzMax Programming/don't bother storing calculated array index in var

skn3(Posted 2012) [#1]
So I was just doing a small test code to see how fast copying an array would be and I came across a weird speed result.
[bbcode]Local size:Int = 10000
Local blah1:Object[size*size]
Local blah2:Object[size*size]
Local x:Int
Local y:Int
Local yCalc:Int
Local index:Int
Local ms:Int

ms = MilliSecs()
For y = 0 Until size
For x = 0 Until size
blah2[(y*size)+x] = blah1[(y*size)+x]
Next
Next
Print "took "+(MilliSecs()-ms)

ms = MilliSecs()
For y = 0 Until size
For x = 0 Until size
index = (y*size)+x
blah2[index] = blah1[index]
Next
Next
Print "took "+(MilliSecs()-ms)[/bbcode]

My result is
took 793
took 1162


That is a 3rd of a second longer for the version that doesn't perform the index calculation twice.... weird! I always assumed that storing the index result in a variable and then using it twice, would be faster.

Last edited 2012

Last edited 2012


skn3(Posted 2012) [#2]
Ah ok,.. so when I turn debug mode off the results play properly!

Nothing to see here :)


Yasha(Posted 2012) [#3]
Second one's faster on my machine.

This is a micro-optimisation though. Ideally the two should be producing the exact same machine code... bit questionable that they aren't actually.

Last edited 2012


zzz(Posted 2012) [#4]
Classic :)

Anyways, unless your already doing it, use superstrict for this along with as local vars as possible. code below is about 30% faster on my machine compared to your code.

also, just for kicks, try switching the x and y loop and watch your cache predictor fail horribly. ~20 times slower for me.




ImaginaryHuman(Posted 2012) [#5]
One thing to consider when storing temporary values in local registers is (although it's faster especially if you're re-using those values) there also needs to be a `write` operation to store the value and then a `read` to retrieve it, since you're explicitly telling it to do so. Yet without storing the temporary value the optimization might turn out to be more efficient at not having to store certain things more than is needed. .. but anyway, overall it does make sense to move as much stuff outside of the loop as possible and into local variables and re-use as much values as you can.


TomToad(Posted 2012) [#6]
Using blah2 = blah1[..]

I get
took 1605
took 1087
took 586

Might get even faster with memcopy()

Edit: with Memcopy() took 254

Last edited 2012