Thread: clock() problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    clock() problem

    Hi all

    I've been using Red Hat Linux gcc to compile my C program.
    I've been trying to measure my function time by using <time.h>


    :insert
    Code:
    #include <time.h>
    int main()
    {
    clock_t t0,t1;
    while(1)
    { t0=clock();
    ...
    ...
    add(a,b)
    ...
    t1=clock()
    printf("%ld,%ld\n",t0,t1);
    }
    }
    Output:

    0,0
    0,0
    ...
    ...
    10000,10000
    10000,10000
    ...
    ...
    20000,20000
    20000,20000...

    It almost looks as if it's been rounded up to 5th digit.
    This result stays doesn't matter how far apart i put the two clock() points.

    Could you give me a suggestion on what I might be doing wrong?

    Thank you very much.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    Still problem...

    Thanks for your reply

    However, when i tried the following code from the tutorial, the output was kept as 0s no matter how long i wait.

    Anyidea?

    I'm using "Linux p21.ecf 2.6.9-55.0.2.ELsmp #1 SMP Tue Jun 12 17:59:08 EDT 2007 i686 i686 i386 GNU/Linux"

    Thanks

    :insert
    Code:
    #include <stdio.h> 
    #include <time.h> 
    
    int main ( void )
    {
      clock_t start, end;
    
      /* Start the timer */
      start = clock();
    
      printf ( "Please wait a few moments and hit return\n" );
      getchar();
    
      /* End the timer */
      end = clock();
    
      /* Print out the difference */
      printf ( "The interval was: %f seconds\n",
        (double)( end - start ) / (double)CLOCKS_PER_SEC );
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It works for me under windows. I get anything from fractions of a second to 3 seconds, depending on how long I wait. I wonder if clock() is measuring the process time used by the program, not run time (it seems like I've seen this referred to as wall time). To check if this is the case, you could print the value of start and end, as well as CLOCKS_PER_SEC:
    Code:
    printf("&#37;ld,%ld %ld\n", start, end, (long) CLOCKS_PER_SEC);
    And see if the difference between start and end is less than CLOCKS_PER_SEC.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Clock() measures CPU time used by the process, not user time spent waiting for the user to type something in.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In Linux, you will definitely get the time spent in the process running, rather than "wall clock" time. In Windows, I beleive it measures "wall time". I believe there is a way to find the processing time in Windows too.

    And yes, the time given by clock is not necessarily so precise that all digits are "used", it may well be a precision of microseconds, but measured in increments of milliseconds or 10 milliseconds - this gives the choice of giving more fine-grained precision later on [e.g. if you configure a different clock-device] without changing the compile-time constant "CLOCKS_PER_SEC". This applies in both Windows and Linux.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM