Results 1 to 6 of 6

Thread: Problem with clock() function in gcc/g++

  1. #1
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Problem with clock() function in gcc/g++

    I just wrote this code:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
    #include <stdlib.h>
    int main() {
            clock_t clk1 = clock();
            printf("%ld\n", clk1);
            int i,r=0;
            for(i = 0; i < 100000; i++) r^=rand();
            printf("%d\n", r);
            clock_t clk2 = clock();
            printf("%ld\n", clk2);
            return 0;
    }
    I also tried using a sleep() before the loop, in both cases, clk1 and clk2 both showing 0, why is that? Am I suppossed aren't they supposed to be showing approximate clock cycle count for the program? What am I doing wrong here?
    Last edited by zobayer1; June 12th, 2013 at 07:54 AM.

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,039
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Problem with clock() function in gcc/g++

    clock() is the processor time actually used, not the "wallclock time", so the delay added by a sleep() is not taken in account (no CPU used while sleeping).

    Your problem is that your test runs too fast for the clock resolution (the output is in microseconds, but the standard doesn't say that you should have microsecond resolution). On my Core I5 laptop, I start to get different clock() values for 500K loops and the clock seems to have a 10ms resolution.

  3. #3
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Problem with clock() function in gcc/g++

    Quote Originally Posted by ofnuts View Post
    clock() is the processor time actually used, not the "wallclock time", so the delay added by a sleep() is not taken in account (no CPU used while sleeping).

    Your problem is that your test runs too fast for the clock resolution (the output is in microseconds, but the standard doesn't say that you should have microsecond resolution). On my Core I5 laptop, I start to get different clock() values for 500K loops and the clock seems to have a 10ms resolution.
    Yah, I had no intention for getting wallclock time, for every query I wanted an incremental value, and thought the clock count could serve the purpose, but I guess you are right, after running around 10^7 - 10^8 loops, I got difference, so the pc is too fast for the algorithm. Can you suggest anything that can help here?

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,039
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Problem with clock() function in gcc/g++

    Try clock_gettime().

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,240
    Distro
    Kubuntu

    Re: Problem with clock() function in gcc/g++

    Or try using gettimeofday().

    Code:
    #include <sys/time.h>
    ...
    
    struct timeval tv;
    
    if (gettimeofday(&tv, NULL) == 0)
    {
        // perform computation(s) using tv.tv_sec and tv.tv_usec
    }
    else
    {
        // error
    }

  6. #6
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Problem with clock() function in gcc/g++

    Quote Originally Posted by dwhitney67 View Post
    Or try using gettimeofday().

    Code:
    #include <sys/time.h>
    ...
    
    struct timeval tv;
    
    if (gettimeofday(&tv, NULL) == 0)
    {
        // perform computation(s) using tv.tv_sec and tv.tv_usec
    }
    else
    {
        // error
    }
    Thanks a lot, tv_usec worked fine for the scenario.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •