Thread: CPU times and clock ticks

  1. #1
    Registered User
    Join Date
    Sep 2001

    CPU times and clock ticks

    hey all,

    im trying to time a system call.

    I want to use/ understand the times() function. but it doesnt seem to work.

    both calls to the times() function return the same value. It must have something to do with the values im passing in but i dont know how...

    possibly b/c they're not inititlised?

    how do i initialise them?

    #include <sys/times.h>
    #include <unistd.h>

    int main()
    clock_t start, end;
    struct tms buffer[4];
    double difference;
    int i;

    start = times(buffer); /*num of clocks time */

    for(i=0; i<1000; i++) /* time 100 times */

    end = (clock_t) times(buffer);
    /* num of clocks time*/

    /*end-start, divided by number of clocks a sec */
    difference = (double) (end - start ) / sysconf(_SC_CLK_TCK);

    difference = difference /1000;
    /*repeated 1000 times */

    printf("time taken: %llf\n", (difference));

    return 1;


    can anyone help me?


    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    1. <> are not code tags, use []

    2. You're using the same buffer for both times, try
    start = times( &buffer[0] ); /*num of clocks time */ 
    for(i=0; i<1000; i++) /* time 100 times */ 
    end = times( &buffer[1] );
    3. maybe your code is so quick that no apparent time has passed. If clock is only updated at say 10 times a second, your program can run for 100mS before you'd be able to measure anything.

  3. #3
    thanks, yeah i tested it and it does take a while to refresh times. (not as accurate as gethrtime).

    I think that you're ment to pass in the same buffer each time.

    just trying to understand why :-)


  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    this is the function I've been using for a while...

    float Time(int flag)
        static clock_t start;
        clock_t end;
        if (flag == START) {
            start = clock();
            return 0.0;
        } else {
            end = clock();
            return (end - start) / CLK_TCK;

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. Approximate number of clock ticks per second
    By black_spot1984 in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 10:46 AM
  3. Clock Class HELP
    By peckitt99 in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2006, 05:03 PM
  4. clock and cycle times' effect on counter time?
    By rexnprogress in forum C++ Programming
    Replies: 0
    Last Post: 11-11-2005, 07:16 PM
  5. world clock
    By nevermind in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 07:45 AM