Help using time.h to calculate running time... 
Author Message
 Help using time.h to calculate running time...

Hi, does anyone know how to use time.h to calculate the running time of a
program?  

Thanks in advance,
Jason



Thu, 13 Sep 2001 03:00:00 GMT  
 Help using time.h to calculate running time...
Thanks a lot for the prompt answers guys, you have been very helpful!


Thu, 13 Sep 2001 03:00:00 GMT  
 Help using time.h to calculate running time...

Quote:

> Hi, does anyone know how to use time.h to calculate the running time of a
> program?

Do you mean processor time or calendar time? These two measurements need not
be the same.

for processor time use clock() gives the best approximation.
for calendar time use time() and difftime().

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
    int i;
    time_t start,end;
    clock_t cend;

    if((start = time(NULL)) == (time_t)-1) exit(EXIT_FAILURE);
    for(i = 0;i < 400;i++) puts("Hello World");
    if((cend = clock()) == (clock_t)-1) exit(EXIT_FAILURE);
    if((end = time(NULL)) == (time_t)-1) exit(EXIT_FAILURE);
    printf("Elapsed processor time is: %.2fsec\n",
        (double)cend / CLOCKS_PER_SEC);
    printf("Elapsed calendar time: %.2fsec\n",difftime(end,start));
    return 0;
    }

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Thu, 13 Sep 2001 03:00:00 GMT  
 Help using time.h to calculate running time...
Try this out.  You may find it helpful for your program:
___________________
I was using Borland 4.51, so you may need to edit this
some depending on your compiler!

// You don't need all of these header files, I was just cutting and pasting
this for you.

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring.h>
#include <dos.h>
#include <conio.h>
#include <graphics.h>
#include <io.h>
#include <math.h>
#include <fcntl.h>
#include <process.h>
#include <alloc.h>
#include <alloc.h>

long main(void)
{

.        // Removed code was here.
.
.
.

int oldhour; int oldminute;
int newhour; int newminute;
string oldampm; string pmhours = "PM"; string amhours = "AM";
string newampm;
struct  time t;   /* to display the time */

.        // Removed code was here.
.
.
.

gettime(&t);
   if (t.ti_hour >= 13)
   {
     oldhour = t.ti_hour - 12;
     oldminute = t.ti_min;
     oldampm = pmhours;
   }
  else
   {
     oldhour = t.ti_hour;
     oldminute = t.ti_min;

         if (t.ti_hour >= 12)
          {
          oldampm = pmhours;
          }
         else
          {
          oldampm = amhours;
          }

   }

.        // Removed code was here.
.
.
.

cout << "\n\n     START TIME: " << oldhour << ":" << oldminute << oldampm <<
endl;

gettime(&t);
  if (t.ti_hour >= 13)
   {
     newhour = t.ti_hour - 12;
     newminute = t.ti_min;

     cout << "COMPLETION TIME: " << newhour << ":" <<
     newminute << "PM" << endl;

   }
  else
   {
     newhour = t.ti_hour;
     newminute = t.ti_min;
     cout << "COMPLETION TIME: " << newhour << ":" <<
     newminute;

         if (t.ti_hour >= 12)
          {
          cout << "PM" << endl;
          newampm = "PM";
          }
         else
          {
          cout << "AM" << endl;
          newampm = "PM";
          }

Quote:
}

.        // Removed code was here.
.
.
.

return 0;

Quote:
}



Fri, 14 Sep 2001 03:00:00 GMT  
 Help using time.h to calculate running time...


Quote:
>Try this out.  You may find it helpful for your program:
>___________________
>I was using Borland 4.51, so you may need to edit this
>some depending on your compiler!

>// You don't need all of these header files, I was just cutting and pasting
>this for you.

>#include <iostream.h>

If you are going to post code to comp.lang.c at least post code that is
written in C.

Quote:
>#include <stdio.h>
>#include <stdlib.h>
>#include <cstring.h>
>#include <dos.h>
>#include <conio.h>
>#include <graphics.h>
>#include <io.h>
>#include <math.h>
>#include <fcntl.h>
>#include <process.h>
>#include <alloc.h>
>#include <alloc.h>

>long main(void)

I hop[e this is a troll. :-)

--
-----------------------------------------


-----------------------------------------



Fri, 14 Sep 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Calculating remaing time from totaltime and current time

2. HELP! Using Run-time libraries

3. Determining type at run-time or compile-time

4. sizeof operator execution - ? run time/compile time

5. Design time / run time

6. Read/Write Properties During Design Time, but Read-Only During Run-Time

7. Design-time vs Run-time mode

8. Timing problem in calling run-time loading DLL

9. Timing problem in run-time loading DLL

10. Design Time/Run Time Licences for OCXs

11. Design time properties at run time...

12. Setting font size of a formview at load time or run time

 

 
Powered by phpBB® Forum Software