Chrono library for high resolution timer

BlitzMax Forums/BlitzMax Programming/Chrono library for high resolution timer

Chapman7(Posted 2016) [#1]
C++11 has a library called Chrono that deals with time/timers. It has a high resolution one that can process microseconds and nanoseconds. Would it be possible to use that with BlitzMax?


TomToad(Posted 2016) [#2]
I posted a hi-res timer module in the code archives. High Resolution Timer Module
It is Windows only. Here is another one High Resolution Timers. You might find others with a search.


Chapman7(Posted 2016) [#3]
I need it to be cross platform :/ i guess ill just play around with it and see if I can come up with anything


Chapman7(Posted 2016) [#4]
Okay I think I have a working example but I have to add "-std=c++11" or "-std=gnu++11" to the compiler and I can't figure out how... Do I have to build with command line? I tried linking with import inside bmax but it didn't work


Chapman7(Posted 2016) [#5]
Okay if anyone needs a cross platform high resolution timer, here is the code. I used the latest TDM64 (had to build BlitzMax from source). TDM64 comes with the <chrono> library. I had to use 'ModuleInfo "CC_OPTS: -std=c++11"' for gcc to access the library since its experimental.

Mod name: Pub.Chrono

chrono.bmx:


chronotimer.cpp:


Example code:
Import Pub.Chrono

Print NanoSecs()
Print NanoSecs()
Print NanoSecs()
Print NanoSecs()
Print NanoSecs()

Print "-------"

Print MicroSecs()
Print MicroSecs()
Print MicroSecs()
Print MicroSecs()
Print MicroSecs()

Output:
69903421
69928367
69936682
69944381
69951772
-------
69965
69971
69978
69985
69992


Unlike MilliSecs(), MicroSecs() and NanoSecs() get the time since the application has started, not since the system has started.


skidracer(Posted 2016) [#6]
So from your results, TDM64's implementation is a fail and you will get better accuracy with the Window's Performance Counter which typically has a frequency of 16mHz or better. It would be more clear if your test called Print NanoSecs() three times in a row to illustrate.

On Linux clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) will give you a more optimal method of sampling without the C++11 hassle.


Chapman7(Posted 2016) [#7]
How do you figure it's a fail?
Edit: Seems like you might be correct, ill have to look at it in a little bit.


skidracer(Posted 2016) [#8]
This suggests steady_clock option in your code may work better on Windows.


Chapman7(Posted 2016) [#9]
Haha that did it. I was playing around with the c++ code in linux and couldn't for the life of me figure out why it was good there but not when I moved it over and ran it in BMax. I updated the code and the example code/output. I appreciate you helping me out


Chapman7(Posted 2016) [#10]
Updated the code so the functions produce a double (they were producing and int and it would continually rollover)

I think you can use NanoSecs() for about 38 months before you start losing accuracy... Don't quote me on that.