Question:
time measurement in for processes in C?
2014-05-12 11:07:05 UTC
I'm wanting to get THE FINEST granulation of time measurement using C to check on the performance of some functions im working on. I'd like something below ms in measurement, what would you recommend?
Three answers:
?
2014-05-13 14:02:15 UTC
You need to describe the platform you are using that has 10ms resolution. On DOS systems using the 8254 timer, the interrupts took place at about 18.2Hz. Which is even worse than 10ms. But it is EASY to reprogram them (and/or simply poll the timer counter) and get MUCH better resolution for a specific case. If you still wanted DOS to keep the date/calendar correct, you had to add more code to make sure this was still handled correctly, of course. So one possibility is to just go find out what kind of hardware is in use, reprogram things, and move on with your timing. Sometimes, this is pretty easy since you can grab the timer hardware just before starting the code, fire off the code, then release the timer hardware back to its normal purposes afterwards.



Another method I've used when I was more interested in quick results and less interested in custom hardware modification just to get a result is to invoke the code over and over and over again, either by calling it over and over or else by wrapping the code in a loop. Then just divide the time I get by the number of iterations to get an estimate. It's usually pretty good, if you execute the code enough to get a decent mean. Under DOS, for example, using the crappy 18.2Hz events, I might ratchet up the loop around the code of interest until I see times of about one minute or so. When I get to that point, the uncertainty of 55ms against 60 seconds was often "good enough" for my use. If I need really fine resolution, I would make that an hour instead.
Jeroonk
2014-05-12 19:20:21 UTC
That depends a little bit on the platform you're working with.



On Windows you can use the high-precision performance counter (http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408.aspx#examples_for_acquiring_time_stamps ), which usually has a sub-microsecond resolution.



On Linux systems, have a look at "clock_gettime" (http://man7.org/linux/man-pages/man2/clock_gettime.2.html ).
IKNOWALL
2014-05-12 19:55:39 UTC
Your some what out of luck in getting better time value at the C library. I know this because I used to write a subtitling software which requires timing to be precise but I found that to get around the time limitation on the software, you have to start using the directX or some hardware specific timing function to get access to the system clock.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...