Timing C program always returns 0 seconds.

This is a discussion on Timing C program always returns 0 seconds. within the C Programming forums, part of the General Programming Boards category; Hi, I was looking at the programming FAQ and I saw an example method of timing a program in c. ...

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    Timing C program always returns 0 seconds.

    Hi,

    I was looking at the programming FAQ and I saw an example method of timing a program in c. So, I tried out the example below and compiled it using the plain gcc under ubuntu. eg.
    gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5). After running the compiled executable from below - I always get 0.000 seconds despite waiting a few seconds before hitting return. Would I have overlooked something?

    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;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    clock() measures CPU time used by the program.
    The seconds which tick by waiting for the user to type in something don't count to this total.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    May be time function would get you the actual time. But you could get the time in terms of second. Perhaps check the following code.

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > clock_t start, end;
    time() returns time_t, not clock_t

    > ( end - start ) / (double)CLOCKS_PER_SEC );
    Use difftime() to calculate the difference between two times in seconds.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    If you want to try the clock() difference on a real-world example, I provide you with my code for measuring different sort methods:

    Code:
    #define ARRAY_SIZE 10000
    
    void swap(int *a, int *b)
    {
        int tmp;
        
        tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    void bubbleSort(int *array, int size)
    {
        int swapped = 0;
        int x,tmp;    
        do
        {
            swapped = 0;
            for (x = 0; x < size-1;x++)
            {
                if (array[x]>array[x+1])
                {
                    swap(&array[x], &array[x+1]);
                    swapped = 1;
                }
            }
            
        } while (swapped);
    }
    
    int main(int argc, char *argv[])
    {
        int array[ARRAY_SIZE];
        int i;
        clock_t start, end;
        
        /* initialize array with ARRAY_SIZE random numbers */
        srand(time(NULL));
        for (i = 0; i < ARRAY_SIZE; i++)
        {
            array[i] = rand(); // random number between 0 and RAND_MAX
        }
        /* start the timer */
        start = clock();
        bubbleSort(array, ARRAY_SIZE);
        end = clock();
        
        /* Print out the difference */
        printf ( "The interval was: %f seconds\n", (double)( end - start ) / (double)CLOCKS_PER_SEC );
    
        getchar();
        return 0;
    }
    Last edited by KONI; 03-24-2007 at 09:44 AM.

  6. #6
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by Salem
    clock() measures CPU time used by the program.
    The seconds which tick by waiting for the user to type in something don't count to this total.
    Aaah yes - thanks! So, the code above is correct for cpu time usage rather than measure the time passed since start and end.

    So to really test the above, I would need to put something cpu intensive enough in a for loop or something to get a measure of cpu time usage.

    And thanks Salem - that gives me the actual time passed between start and finish.

    I guess from a program duration pov we are interested in the cpu time taken to run a program - at least that what I was trying to do.

  7. #7
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    That's nice of you Koni. Appreciated! I will have some fun playing with the timings. Makes a change friom SQL tuning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. Inserting text into MDI program
    By Rutabega in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2005, 10:25 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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