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
LVL 1
Who is Participating?

Commented:
Oops, I almost forgot about 'clock()':

/* CLOCK.C: This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/

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

void sleep( clock_t wait );

void main( void )
{
long    i = 600000L;
clock_t start, finish;
double  duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

0

Commented:
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 );
}

0

Author Commented:
wonderful, quick response and exactly the answer as what I need, thx.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.