Testing Speed...

BlitzMax Forums/BlitzMax Beginners Area/Testing Speed...

po(Posted 2007) [#1]
If I want to compare how fast two chunks of code are (say two methods), what would be the best way to do this?


Gabriel(Posted 2007) [#2]
That depends what the code does, when you call it, how often you call it, and probably several other factors. However, in general,the best way to compare it is to call them how you use them. If you don't call them a million times in a loop, don't be misled into testing that way. If the methods are too quick to test accurately in one loop, keep a total of the time taken each call and keep adding to it over a period of time. If you call them while other things are happening, don't forget to make sure those same things are happening in your test too. And use something more accurate than Millisecs() like QueryPerformanceCounter.

Frankly, if the project isn't finished, I'd code around them, leave both in, and test when the project is finished because that's always the best time to optimize if you're unsure which is going to be faster. The only time I ever optimize early is when I know (a) is slow and I know (b) is always going to be faster, and also that (b) isn't going to be any less readable or maintainable than (a).


tonyg(Posted 2007) [#3]
If it's a straight 2 methods compare use ms although you might have to test it in a loop. Gab is right about 'silly' tests but take a good guess how often you're likely to call a method.
It's easy to keep adding 0s to a loop and be happy when there's a couple of ms difference when you get up to millions.
Alternatively, use/create a profiler like this .
Finally, taken into account things like readability, maintainability, portability and robustness. Unless you're REALLY looking for speed doing something a nice way can be worth a couple of ms when compared with doing something a quick but horribly complex way.


po(Posted 2007) [#4]
Ah, ok. In that case quick question: Which is faster?

x1=32000
x2=32000
y1=32000
y2=32000

d=Sqr((x2-x1)^2+(y2-y1)^2)

Return d/32

OR

x1=32000/32
x2=32000/32
y1=32000/32
y2=32000/32

d=Sqr((x2-x1)^2+(y2-y1)^2)

Return d


tonyg(Posted 2007) [#5]
How many times are you expecting to call them?
Basically, in this case, it doesn't matter unless your doing millions of calls.
If you're desperate to save time try this :
d=Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
Quicker but more readable?
<edit> and still only saves over millions of repetitions.


H&K(Posted 2007) [#6]
The second one. (without testing). Because it has less operations of a similar nature. They are not really two differnt functions.

They are the same function that has then been optimised.


po(Posted 2007) [#7]
Wait, so 5*5*5 is faster than 5^3?


tonyg(Posted 2007) [#8]
By about 1ms over a million loops.


po(Posted 2007) [#9]
Ok. I only call the method a few times every click.