Link to home
Start Free TrialLog in
Avatar of PX
PX

asked on

Calculate time diff

Hi,

My question is how can I write a function says like stop watch, time start, then stop, I want to get the time in mil seconds between that period.

thx
Avatar of jkr
jkr
Flag of Germany image

Use 'difftime()', e.g.

/* DIFFTIME.C: This program calculates the amount of time
 * needed to do a floating-point multiply 10 million times.
 */

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

void main( void )
{
   time_t   start, finish;
   long loop;
   double   result, elapsed_time;

   printf( "Multiplying 2 floating point numbers 10 million times...\n" );
   
   time( &start );
   for( loop = 0; loop < 10000000; loop++ )
      result = 3.63 * 5.27;
   time( &finish );

   elapsed_time = difftime( finish, start );
   printf( "\nProgram takes %6.0f seconds.\n", elapsed_time );
}


ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PX
PX

ASKER

wonderful, quick response and exactly the answer as what I need, thx.