View Full Version : Really really fast timer in C++ or Cocoa?
Hi there..
I need a really fast timer for my own benchmarking, and am having problems getting the results I need with most of the Cocoa stuff I can find online..
There is nothing for any C libraries as far as I can see..
I have also noticed that it appears that there are huge anomalys between models of Macs and what kind of results you get.. That's not such a biggie as long as I can quantify it and get a good 'tick' rate.
Does anyone here know a way to get a quantifiable timer which 'ticks' some 2 or 3,000 times a frame on a Mac?
Cheers,
scratt
EDIT - Even something quick and dirty that gets some kind of hardware timer.. I am not putting this in final code, just using if for my own evil purposes!
Seeing as no one ever answers your questions scratt, I thought I'd have a crack at it :)
gettimeofday(struct timeval *tp, struct timezone *tzp); fills in a timeval struct with the seconds and microseconds since Jan 1, 1970.
You could use this to make your own timer by putting it in a thread and have it poll a value every so often.
#include <sys/time.h>
#include <stdio.h>
main()
{
struct timeval mytime;
gettimeofday(&mytime, NULL);
long temp = mytime.tv_sec;
int i = 0;
long j = 0;
while (1)
{
gettimeofday(&mytime, NULL);
if (mytime.tv_sec > temp)
{
temp = mytime.tv_sec;
printf("Seconds %d \tLoop Iterations %d\n", i, j);
i++;
}
j++;
}
}
This is a proof of concept to show how fast the timer iterates.
For use in cocoa, I imagine you could put this loop inside a thread polling a value or calling a method every second. Also if you wanted to get a microsecond value (rather than seconds) you could reference your timeval struct variable tv_usec, in this example it would be mytime.tv_usec
Try it with the "time" command for accuracy. I used the same method when I had to make a Timer class in java because Java 2 Personal Profile Edition (for Zaurus) doesn't even have a Timer class !
colivigan
2006-04-29, 15:26
Also have a look at the setitimer system call. There is a man page for it. It's not really clear to me what you want to do - if you just want to measure how long something takes to execute, then gettimeofday seems like the right route. If you want to schedule an event to happen via a signal at a certain time, then use setitimer.
The call you are looking for is called UpTime. It has a resolution of roughly nano seconds and is a very fast call (<100ns). It basically queries the frequency counter in the processor. The results needs to be converted using AbsoluteToNanoseconds. I think you need to link the CoreServices framwork.
eg:
UInt64 HslTimer::GetTimeNative64 () {
AbsoluteTime absTime = ::UpTime ();
return ((UInt64 (absTime.hi) << 32) + absTime.lo);
}
UInt64 HslTimer::CalcNanoSeconds (UInt64 native) {
UnsignedWide uw;
uw.hi = native >> 32;
uw.lo = native;
Nanoseconds nano = AbsoluteToNanoseconds (uw);
return ((UInt64 (nano.hi) << 32) + nano.lo);
}
The only problem I had with it was that on dual processor Macs (didn't check the dual core), there seems to be a small but measurable offset between the processors. So for ultra precise measuring, you'd wana disable the second processor. No idea how dual cores behave in this respect.
Thank you all.... I have been away racing and stuff so kind of thought this thread had gone the way of all my other questions!! ;)
I have just got back to work on my sim, after the above mentioned break, and have this weekend clear so will integrate this timer and report back here..
Thanks, once again.. Nice to know *some* people care. :)
billybobsky
2006-05-19, 10:15
No one cares, scratt.
We are just bored out of our fucking minds and thinking is our only way out of this aesthetic veneer of a world...
vBulletin® v3.6.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.