time

This is a discussion on time within the C++ Programming forums, part of the General Programming Boards category; Hi How can i find out the time taken to run each iteration in the program below? Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    time

    Hi

    How can i find out the time taken to run each iteration in the program below?

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
      int n, x = 0;
    
      for (n = 1000; n > 0; n--)
      {
    
        x = n + x;
       // print time taken 
    
      }
      
      cout << x << endl;
    
      return 0;
    }
    Shuo

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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 2007
    Posts
    65
    I tried running the programs. it keeps giving 0.000000 seconds when i waited for a couple of seconds.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you are using ints, that is to be expected. Are you using ints or doubles? Let's see your code.
    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
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It looks like there's a bug in that code.

    This:
    Code:
    printf ( "The interval was: %f seconds\n",
        (double)( end - start ) / (double)CLOCKS_PER_SEC );
    Should probably be this:
    Code:
    printf ( "The interval was: %lf seconds\n",
        (double)( end - start ) / (double)CLOCKS_PER_SEC );
    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!

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    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 )/CLOCKS_PER_SEC );
    
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Still gives the same answer.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Looking....
    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!

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yeah, printf ignores &#37;lf and changes it to %f for you.

    As for your code: it works fine here, compiling with (heaven help me) lcc-win32. I apparently waited 5.820000 seconds. I don't know what else to tell you.

    Although that does remind me: your code is C, and definitely not C++. Be sure you're calling the appropriate compiler.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I get the 0.00000 answer too. Still researching. I'm on a mac.
    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!

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Apparently, on Unix (FreeBSD), clock() returns CPU seconds, not elapsed seconds.

    This is the code I used in one of my apps. It starts off with this at the front:
    Code:
    	// This is for elapsed time calculations. 
    	char hours[3], minutes[3], seconds[3] ; 
    	
    	struct tm start_time ; 
    	struct tm *the_time ;  
    	time_t  stime, etime ; 
    	
    	double elapsed ; 
    	int elap_hour, elap_min, elap_sec ; 
    	
    	time(&stime) ;  // Get start time in seconds since epoch 
    	the_time = localtime(&stime) ; 
    	
    	// Save the Time of Day
    	start_time.tm_hour = the_time->tm_hour ; 
    	start_time.tm_min = the_time->tm_min ; 
    	start_time.tm_sec = the_time->tm_sec ; 
    	// End of elapsed time stuff, for now.
    At the end, I do this:
    Code:
    		time(&etime) ; // Get end time in seconds since epoch 
    		the_time = localtime(&etime) ; 
    	
    		sprintf(hours,   "&#37;02d", the_time->tm_hour ) ; 
    		sprintf(minutes, "%02d", the_time->tm_min  ) ; 
    		sprintf(seconds, "%02d", the_time->tm_sec  ) ; 
    	
    		cout << "End Time:     " << hours << ":" << minutes << ":" << seconds << endl; 
    	
    		sprintf(hours,   "%02d", start_time.tm_hour ) ; 
    		sprintf(minutes, "%02d", start_time.tm_min  ) ; 
    		sprintf(seconds, "%02d", start_time.tm_sec  ) ; 
    	
    		cout << "Start Time:   " << hours << ":" << minutes << ":" << seconds << endl; 
    	
    		// Calculate the elapsed time 						   
    		elapsed = difftime(etime, stime) ; 
    		//printf("Elapsed time is %d %s.\n", (int) elapsed, ((int) elapsed == 1 ) ? "second" : "seconds"  ) ; 
    		elap_hour = (int) (elapsed / (60*60)) ;   // hours  
    		elap_min  = (int) (elapsed / 60) % 60  ;  // minutes 
    		elap_sec  = (int) elapsed % (60) ;        // seconds 
    	
    		// printf("Elapsed (int) time is %10d\n" , (int) elapsed ) ; 
    		printf("Elapsed Time: %02d:%02d:%02d\n", elap_hour, elap_min ,elap_sec ) ;
    (Yes, I know it's ugly - but it works )
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 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