-
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
-
-
I tried running the programs. it keeps giving 0.000000 seconds when i waited for a couple of seconds.
-
If you are using ints, that is to be expected. Are you using ints or doubles? Let's see your code.
-
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 );
-
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;
}
-
Still gives the same answer.
-
-
Yeah, printf ignores %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.
-
I get the 0.00000 answer too. Still researching. I'm on a mac.
-
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, "%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 :))