C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-09-2007, 07:54 AM   #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;
}
Can someone explain why using sleep() or usleep() and clock() together doesn't work. I have also tried using times() instead of clock() and got the same results. I get 0 ticks when I have a sleep() in the loop. I program on Linux, are there any other functions that measure time in millisecond precision. The reason for having a usleep() in the loop is to reduce cpu usage. I only need millisecond, so I put a usleep(10000) in the loop to sleep for 0.01 sec. I know someone will ask why do I need to use clock(), why not just have a float elapsed variable in the loop and just do a elapsed+=0.01; This is a simple loop, I need to use this in more complex loops where there are if statements, syslogs, assignments, calculations... and I cannot be sure how long those operations take.

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   Reply With Quote
Old 07-09-2007, 08:14 AM   #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   Reply With Quote
Old 07-09-2007, 08:24 AM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
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".
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 07-09-2007, 10:12 AM   #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   Reply With Quote
Old 07-09-2007, 11:13 AM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by aleksgidenko View Post
Can someone explain why using sleep() or usleep() and clock() together doesn't work.
Because it's not supposed to. clock() only counts ticks when your program is running. When your program is sleeping, it isn't running, so the clock doesn't tick. I have no idea why you'd want to measure a sleep() anyway -- you already know how long it is.

Quote:
BASICALLY... can anyone recommend a reliable way of measure time in millisecond precision, on linux in c/c++.
gettimeofday(). This gives TRUE times, regardless of whether your process sleeps or not.
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:08 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22