timing the C

This is a discussion on timing the C within the C Programming forums, part of the General Programming Boards category; Hi, Just a quick one by way of a short story I started off by finding out the execution time ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    timing the C

    Hi,

    Just a quick one by way of a short story

    I started off by finding out the execution time of my c programs using the the time_t variables in time.h.

    These only seemed to record a minimum of seconds which was OK when my programs took many minutes, and even hours sometimes :-)

    Anyhow, the resolution is much too coarse so when I started MPI programs, well I was much happier with the very precise timings you could get off that library.

    So, if with sequential programs I took to using MPI, only for the timer. I've done worse things ...

    However I found sys/time.h in my include directory and it doesn't have milliseconds res. but it has microseconds. That's pretty good. I get the time of day in secs and milliseconds and then I subtract a new reading of the time day when all my operations are finished.

    So question is, does that all sound OK? I mean, I can't be accused of a cheap hack anymore?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here's what I do. Watch your calcs at the end.

    Code:
    	int rc ; 
    	
    	struct timeval tv_start, tv_end ; 
    	struct timezone tz ; 
    
    	rc = gettimeofday(&tv_start, &tz) ; 
    	if (rc) { 
    		printf("gettimeofday() failed, rc = %d\n", rc) ; 
    		return rc ; 
    	} 
    
    // do something... 
    
    	rc = gettimeofday(&tv_end, &tz) ; 
    	if (rc) { 
    		printf("gettimeofday() failed, rc = %d\n", rc) ; 
    		return rc ; 
    	} 
    
    	printf("tv_start = %ld.%ld, tv_end = %ld.%ld\n", tv_start.tv_sec, tv_start.tv_usec, tv_end.tv_sec, tv_end.tv_usec) ; 
    	if ( tv_end.tv_usec < tv_start.tv_usec) { 
    	//	printf("tv_start_usec - tv_end_usec = %d\n", tv_start.tv_usec - tv_end.tv_usec) ; 
    		tv_end.tv_sec-- ;            // borrow from seconds 
    		tv_end.tv_usec += 1000000 ;  // a usec is 1 milllionth of a second. 
    	} 
    	printf("Elapsed Time (seconds) was %ld.%06ld\n", (tv_end.tv_sec- tv_start.tv_sec), (tv_end.tv_usec - tv_start.tv_usec))  ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    thanks Dino.

    I do the "calcs" as you put it like this

    Code:
    gettimeofday(&tv, &tz); secs=tv.tv_sec; misecs=tv.tv_usec;
    which is perfectly readable
    Code:
    gettimeofday(&tv, &tz); secs=(tv.tv_sec - secs)*1000000+(tv.tv_usec - misecs);
    	printf("Op time: &#37;8.6f secs.\n",(double)secs/1000000);

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by stabu View Post
    thanks Dino.

    I do the "calcs" as you put it like this

    Code:
    gettimeofday(&tv, &tz); secs=tv.tv_sec; misecs=tv.tv_usec;
    which is perfectly readable
    Code:
    gettimeofday(&tv, &tz); secs=(tv.tv_sec - secs)*1000000+(tv.tv_usec - misecs);
    	printf("Op time: &#37;8.6f secs.\n",(double)secs/1000000);
    My head hurts. Explain, with your calculations, how a start time of 1.999999 and an end time of 2.000001 will yield the right time. In your case, won't tv.tv_usec - misecs yield a negative value?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would expect (2-1)*1000000 + (1-999999) to evaluate to 2, assuming variables are accurately named.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Oh. Yeah. I am splitting the calcs and keeping them split, and the OP is merging them into a final value at the end.

    Thanks tabstop.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    Unhappy

    Sorry for giving you head pain! Least of all to someone who has offered advice.
    which is perfectly readable
    I was being ironic, I left out the similies.
    I'll correct my etiquette in future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 03:10 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 04:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 01:43 AM

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