High resolution timer via C++ ?

BlitzMax Forums/BlitzMax Programming/High resolution timer via C++ ?

I created this module a while ago. Works only with Windows.
Module toad.counter
Extern "Win32"

Function QueryPerformanceCounter(lpPerformanceCount:Long Var)
Function QueryPerformanceFrequency(lpFrequency:Long Var)

End Extern

Function GetTicks:Long()
	Local Ticks:Long
	
	If QueryPerformanceCounter(Ticks)
		Return Ticks
	Else
		Return 0
	End If
End Function

Function GetMillisecs:Double()
	Global Firstcall:Int = True
	Global Frequency:Long
	Local Ticks:Long
	
	If Firstcall
		QueryPerformanceFrequency(Frequency)
		Firstcall = False
	End If
	
	If QueryPerformanceCounter(Ticks)
		Return (Double(Ticks * 1000) / Frequency)
	Else
		Return 0
	End If
End Function

Put it in BlitzMax/mod/toad.mod/counter.mod/counter.bmx
You then use "Import toad.counter" at the top of your source.
GetTicks() will return the number of ticks since program start. The meaning of ticks is machine dependent, so it is best used for profiling and such.
GetMillisecs() will give you the number of milliseconds passed with double precision. This is great for more accurate timing in your code.
Note that not all machines support high resolution timers, especially older ones.