High resolution timer via C++ ?
BlitzMax Forums/BlitzMax Programming/High resolution timer via C++ ?
| ||
I'm not sure we have access to a real high resolution timer natively in BMax. Can someone expound on this and if we don't, something should be workable in C++ I suppose. |
| ||
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. |
| ||
Note that not all machines support high resolution timers, especially older ones. Thanks Tom. Does this mean that older machines will crap out using this method? Or will they return the default precision? |
| ||
default precision. My machine returns timings to some stupid precision but you wont actually know for sure if it is accurate. My version; http://www.blitzbasic.com/codearcs/codearcs.php?code=1909 |
| ||
My code will return 0 if high resolution timers are not supported. Nowadays, it will be very rare to find a machine that doesn't support them. I'm not even sure if there is a DirectX7 capable machine that doesn't also support high resolution timers, so you should be safe with them. If you want to be extra sure, you could always test first and set a flag. Local HRFlag:Int = True local TestTime:Long = GetTicks() If Not TestTime Then HRFlag = False While GameIsRunning Local Time:Double If HRFlag Time = GetMillisecs() 'Call High Resolution Timer Else Time = Millisecs() 'Call BMax Timer End If Wend |
| ||
Does anyone know if it's possible to use a high resolution timer on MacOS and Linux, and if so which calls are quired? I've put together a little module that will setup a high-resolution timer, but if the timer is not supported or the platform os MacOS/Linux it will transparently use MilliSecs() instead so you can use it without worrying about platform. Ideally of course I'd like to support high-resolution timers on all platforms... Muttley |
| ||
Does anyone know if it's possible to use a high resolution timer on MacOS and Linux Yes. and if so which calls are (re)quired? I don't know off-hand. Try Google, or your favourite search engine :-) |
| ||
Good to know Brucey, but Google failed to deliver which is why I posted here. :) |
| ||
Some Linux info here : http://lwn.net/Articles/167897/ |
| ||
And some OS X info here : http://www.macresearch.org/tutorial_performance_and_time Interestingly, the Linux link was atop the first page of the Google search, the Mac link, towards the bottom of the first page of that particular Google search.... |
| ||
Thanks for that, but why the attitude? Just because you happened to pick a better search term for Google than I did... BTW, that Linux one is only really relevant for Kernels >= 2.6.16. |
| ||
2.6.16+ has the best real-time support. (although I believe there are kernel patches for earlier versions) Some more information : http://www.timesys.com/timesource/real_time.htm |