Smaller Time Units the MilliSecs()

BlitzPlus Forums/BlitzPlus Programming/Smaller Time Units the MilliSecs()

Grey Alien(Posted 2005) [#1]
Sorry, that's smaller time units "than" millisecs()

Hi, some of the routines I am writing are very fast and I can't measure how long they take anymore with MilliSecs(). In C I used to be able to access ticks (or something) which was much smaller than milliseconds. Anyone know of a similar thing in Blitz? I know I could call my functions 1000 times and measure the total time in Millisecs but this seems a bit lame.

thanks in advance.


sswift(Posted 2005) [#2]
If you want to profile a function that takes less than a millisecond, you can still use milliseconds to time it, assuming it gets called many times.

If your function AREN'T getting called hundreds of times in your code, and they take less than a millisecond each, it is insane to bother optimizing them. :-)

The trick is that if you record the start and end time each time your function is called, and increment the counter based on the difference in times, then sometimes your functon will be called just as the timer is about to tick over one, and sometimes it won't.

The secret is that the more often it ticks over during your function, the slower the function is.

So if you have a function that takes half a millisecond to execute, and you record all the time differences before and after it, and then divide the total by the total time your program ran, then that will tell you how much time the function took.

Ie:

Const TIMER_BLAH = 0
Dim Timer(1)

ProgramStartTime = Millisecs()

Repeat

   FunctionStartTime = Millisecs()
   Blah()
   Timer(TIMER_BLAH) = Timer(TIMER_BLAH) + (Millisecs()-FunctionStartTime)

Until KeyHit(1)

ProgramEndTime = Millisecs()
ProgramTotalTime = ProgramEndTime - ProgramStartTime 

BlahTotalTime# = Float(Timer(BLAH)) / Float(ProgramTotalTime)

Print BlahTotalTime#
WaitKey()



Grey Alien(Posted 2005) [#3]
OK, I get it thanks. one change though, this line
BlahTotalTime# = Float(Timer(BLAH)) / Float(ProgramTotalTime)

should use the TIMER_BLAH constant not BLAH which will be 0 (unitialized)

thanks for the tip!