![]() |
| | #1 |
| Registered User Join Date: Jul 2007
Posts: 2
| using clock() with sleep() Code: #include <iostream>
#include <unistd.h>
#include <time.h>
using namespace std;
int main(void)
{
clock_t start, end;
start = clock();
while (true)
{
usleep(10000);
end = clock();
cout << "ticks=" << end-start << endl;
cout << "elapsed=" << ((end-start)/(CLOCKS_PER_SEC*1.0)) << " secs" << endl;
}
return 0;
}
BASICALLY... can anyone recommend a reliable way of measure time in millisecond precision, on linux in c/c++. i have been searching for a reliable method for a while now. |
| aleksgidenko is offline | |
| | #2 |
| . Join Date: Nov 2003
Posts: 293
| It does work, but probably not as you expect. Find out what CLOCKS_PER_SEC is on your box. One of the best ways to reduce cpu usage is to call nice(). This reduces the impact on everything else and assures your code will run whenever nobody else wants the cpu. Not when you decide you want to "wake up" and run. gettimeofday() has high resolution. |
| jim mcnamara is offline | |
| | #3 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| usleep() wastes real time, and clock() measures CPU time. Also, all the sleep functions (AFAIK) only guarantee a minimum amount of time sleep, not an absolute amount of time. sleep(1) returning after 10 seconds would be perfectly within spec for example. On most useful systems, like the one you're using, sleeping doesn't burn away CPU time at the same rate. > can anyone recommend a reliable way of measure time in millisecond precision gettimeofday() has a precision of 1uS. Note that this isn't the same as being accurate to 1uS. If back-to-back calls to gettimeofday() show a small increase in the uS time, then you could probably use it. Failing that, if your machine is pentium based, then do a board search for "rdtsc". |
| Salem is offline | |
| | #4 |
| Registered User Join Date: Jul 2007
Posts: 2
| Thanks, gettimeofday() works great and isn't affected by sleep(). Not sure why I've never tried it before. For my purposes, I only need millisecond precision, so gettimeofday() should help me get pretty accurate results. I will also look into nice() instead of sleep() to reduce cpu usage. I will look into the documentation of gettimeofday() for more info... but, does anyone know any conditions that can make gettimeofday() inaccurate? |
| aleksgidenko is offline | |
| | #5 | ||
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
Quote:
| ||
| brewbuck is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Logical Error in Clock program | SVXX | C++ Programming | 0 | 05-10-2009 12:12 AM |
| Outside influences on clock cycles? (clock_t) | rsgysel | C Programming | 4 | 01-08-2009 06:15 PM |
| clock program | bazzano | C Programming | 3 | 03-30-2007 10:12 PM |
| why do we require sleep? | jinx | A Brief History of Cprogramming.com | 43 | 07-14-2004 08:21 AM |
| Sleep is overrated... | Polymorphic OOP | A Brief History of Cprogramming.com | 24 | 01-24-2003 12:40 PM |