Negative time when timing execution (time_t, clock())

This is a discussion on Negative time when timing execution (time_t, clock()) within the C Programming forums, part of the General Programming Boards category; Hi, I am using the clock() function to time an operation entitled cycleThroughPermutations. I have used it for the start ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Unhappy Negative time when timing execution (time_t, clock())

    Hi,

    I am using the clock() function to time an operation entitled cycleThroughPermutations.

    I have used it for the start and end time. However, I get negative times when the process takes a while.

    My code is below.

    PHP Code:

    /* cycle through the permutations and keep time, parameters include distance matrix, dimensions of graph, shortest cycle pointer, least distance measure, staring perm list and stop index */
    startTime clock();
    cycleThroughPermutations(distanceMatrixdimensionsOfGraphshortestCycle, &leastDistanceMeasurepermListleaveAloneIndex);
    endTime clock();
    timeDifference = (double)(endTime startTime)/(double)CLOCKS_PER_SEC
    I think it may have something to do with the clock() function. When I used the following code, I never had this probelm.

    PHP Code:
    time(&startTime);
    cycleThroughPermutations(distanceMatrixdimensionsOfGraphshortestCycle, &leastDistanceMeasurepermList, -1);
    time(&endTime);
    timeDifference difftime(endTime,startTime); 
    Can someone explain what is the problem?


    Thanks in advance.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    clock returns -1 upon error, so that could be it. But I'm not particularly confident that that is the problem. How about you check ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Oh goody, another game of guess the OS and compiler.

    Any particular negative value, or just "randomness"?

    Does it jump from being fairly large to being a fairly large negative number with a small increase in the amount of work?

    Maybe it's just arithmetic overflow with the short datatypes and your archaic compiler.

    Are you aware that clock() can return -1 ?
    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.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Platform information is

    Memory = 2015 MB
    Disk = 84774784 MB
    Cpus = 1
    Arch = "INTEL"
    OS = "LINUX" (Red Hat Linux 9)

    I have captured an easy example of my problem.

    The following code below . . .

    Code:
    #include <stdio.h> 
    #include <time.h> 
    
    int main ( void )
    {
      int i;
      clock_t start, end;
      double timeDifference;
    
      start = clock();
    
      for ( i = 0; i < 100000000; i++ )
        ; /* Busy wait, how long doesn't matter */
    
      end = clock();
      
      timeDifference = (double)( end - start ) / (double)CLOCKS_PER_SEC;
    
      printf ( "The loop started at %f and ended at %f, took %f seconds\n", start, end, timeDifference);  
    
      return 0;
    }
    outputs the following.

    Code:
    The loop started at 0.000000 and ended at 0.210000, took -1.545037 seconds
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Well for starters, start and end are integral (most likely), so using %f to print them will just give you bogus information.

    Try say
    printf( "%lu %lu\n", (unsigned long)start, (unsigned long)end );
    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.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Is there any way I can get measurement units that are less than a second (i.e. millisecond)?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This works in Windows console/Dos mode:


    Code:
    #include <time.h>
    #include <stdio.h>
    #include <dos.h>
    
    int main(void)
    {
       clock_t start, end;
       start = clock();
    
       delay(2000);
    
       end = clock();
       printf("The time was: %f\n", (end - start) / CLK_TCK);
    
       return 0;
    }
    Did you forget to include the right files, perhaps?

    I'm unclear why you are casting any variables??

    Windows is NOT a real time OS - millisecond timings are possible, but entirely meaningless. Tenths are good, 100's of a second timings are barely so-so, imo.

    It all depends on what else the OS is doing at that (exact) time, and how many times you're willing to re-run it, to see a mean converge on the elapsed time.

    I can't help you at all, with Linux.

    Adak
    Last edited by Adak; 10-23-2006 at 08:53 PM.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    printf ( "The loop started at %f and ended at %f, took %f seconds\n", start, end, timeDifference);
    As has been mentioned, %f is giving you a bogus answer for start and end.

    Really, your problem is it's giving you a bogus answer for timeDifference. %f is for floats, not doubles.

    Code:
    printf ("%ld %ld %lf, start, end, timeDifference);
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by QuestionC
    %f is for floats, not doubles.
    No, %f in printf() is for both floats and doubles. See link:
    Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf?

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    From the man page for clock() (on Linux) ...

    Note that the time can wrap around. On a 32bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.
    Also, on Redhat 9, clock_t (ultimately) is a long. So your printf() should be

    Code:
    printf ( "The loop started at %ld and ended at %ld, took %lf seconds\n", start, end, timeDifference);
    Though of course that is not very portable.

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by SKeane
    So your printf() should be

    Code:
    printf ( "The loop started at %ld and ended at %ld, took %lf seconds\n", start, end, timeDifference);
    Again, %lf is wrong in printf(). See my previous post.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > Is there any way I can get measurement units that are less than a second (i.e. millisecond)?
    Wait a minute, first it was why is it negative (have you fixed this by the way by not doing dumb stuff)?
    Now it's how do I get high precision.

    I do wish people would get to ask their real question.

    Answer:
    Lookup gettimeofday()
    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.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by noodles
    I question the FAQ on that one
    1000 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double.

    1001 These are called the default argument promotions.

    1002 If the number of arguments does not equal the number of parameters, the behavior is undefined.

    1003 If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined.
    http://c0x.coding-guidelines.com/6.5.2.2.html

    I don't know about C89 however.
    Callou collei we'll code the way
    Of prime numbers and pings!

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > How can printf use %f for type double, if scanf requires %lf?
    Like the link says, "Due to the ``default argument promotions'' (which apply in variable-length argument lists".
    Parameters which match the "..." part of the function prototype (all the parameters in printf and scanf past the control string) undergo a different kind of argument promotion. One of these being that all floats are converted to double (so printf never actually sees a float value, only double).

    scanf on the other hand has to deal specifically with "pointer to float" and "pointer to double" as different cases (hence the two formats), because floats and doubles are stored in different amounts of memory, and have different formats. You can't just chop a double in half and get a float.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. 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
  4. how to make the prog. read time from pc clock
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2002, 02:50 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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