Smaller Time Units the MilliSecs()

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

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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()