Thread: using clock() with sleep()

  1. #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.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #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?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 12:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. clock program
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 10:12 PM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM