Comparing time taken for execution

This is a discussion on Comparing time taken for execution within the C Programming forums, part of the General Programming Boards category; I was comparing time taken by different sorting functions. I was using the line of code below.However i have a ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    54

    Question Comparing time taken for execution

    I was comparing time taken by different sorting functions. I was using the line of code below.However i have a question, is there a better way to calculate time, maybe a self made "for" statement, how would i start this? By the way in what is the code below calculating time, in seconds,milliseconds..? Im not sure, any help would be appreciated lads. Im just looking for something better

    Code:
    clock_t start = clock();
                bubbleSort(array, SIZE ); //calls bubblesort to sort 1st copy
                printf("Time elapsed: %f seconds \n", ((double)clock() - start) / CLOCKS_PER_SEC);

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    clock()- start is in CLOCKS, which is ... well, who knows (system-specific). So:
    Code:
             CLOCKS              SEC
    CLOCKS / ------ = CLOCKS * ------ = SEC
               SEC             CLOCKS

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There are usually OS specific clock functions that are more precise than the clock_t specification.

    In Windows I know its most accurate clock is like 1x10^-6 decimal places of precision.... Though it actually does do rounding.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by master5001 View Post
    There are usually OS specific clock functions that are more precise than the clock_t specification.

    In Windows I know its most accurate clock is like 1x10^-6 decimal places of precision.... Though it actually does do rounding.
    Would be looking for linux OS preferably! Any other one??

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    clock()- start is in CLOCKS, which is ... well, who knows (system-specific). So:
    Code:
             CLOCKS              SEC
    CLOCKS / ------ = CLOCKS * ------ = SEC
               SEC             CLOCKS
    I would like something for Linux OS preferably!

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't know off hand... I am sad to say even though I have not written many win-apps in a while, and have done more linux stuff as of late, I have not needed to do anything with a super accurate clock. Though imo the standard time functions are give or take correct within a couple miliseconds or better. Is that actually not accurate enough for you?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by master5001 View Post
    I don't know off hand... I am sad to say even though I have not written many win-apps in a while, and have done more linux stuff as of late, I have not needed to do anything with a super accurate clock. Though imo the standard time functions are give or take correct within a couple miliseconds or better. Is that actually not accurate enough for you?
    lol, no i think its good enough. Was just wondering if there are other ways, or different functions for gettin time execution on like what i applied it to above.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would use time(). Which returns the number of miliseconds since January 1, 1970.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by master5001 View Post
    I would use time(). Which returns the number of miliseconds since January 1, 1970.
    Errm sorry lad, how would i use that in the code i posted at the begining. To calculate the time taken for execution you know, then compare between bubble and selection sort.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    clock gives the amount of time since <start of program>.
    time gives the amount of time since <Jan 1 1970>.
    For your purposes, they are therefore equivalent, as in if you search-and-destroy clock with time, and remember that your answer is in milliseconds, then you're done.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    clock gives the amount of time since <start of program>.
    time gives the amount of time since <Jan 1 1970>.
    For your purposes, they are therefore equivalent, as in if you search-and-destroy clock with time, and remember that your answer is in milliseconds, then you're done.
    Umm ok, i understand your first two lines, but how would the actual coding look for the time ();

    Could you show me a link to an example, or something pls lad!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Have you never done a program with randomness in it? Never done srand(time(0))?

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    Have you never done a program with randomness in it? Never done srand(time(0))?
    Oh, i didnt notice, actually i used in the program i was talking about, sort random numbers

    Code:
     srand( time( NULL ) ); /* seed the rand function */
       
       for ( i = 0; i < SIZE; i++ )
          copy3[i]=_2ndcopy[i]=_1stcopy[i]=array[i] = rand() % 90 + 10; /* give each element a value */
    But for time, calculating time, i really dnt understand how'd id set it to this code

    Code:
    clock_t start = clock();
                bubbleSort(array, SIZE ); //calls bubblesort to sort 1st copy
                printf("Time elapsed: %f seconds \n", ((double)clock() - start) / CLOCKS_PER_SEC);

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So change clock() to time(NULL).

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    So change clock() to time(NULL).
    I did something like this, but i got a negative number.

    Code:
    rintf( "\n\n" );
       clock_t start = time(NULL);
       bubbleSort( array, SIZE );
       printf("Time elapsed: %f seconds \n", ((double)clock() - start) / CLOCKS_PER_SEC);
       printf( "Sorted array:\n" );

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM

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