Solved

# Calculate time diff

Posted on 2002-06-20
1,001 Views
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
0
Question by:PX
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 86

Expert Comment

ID: 7096338
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

LVL 86

Accepted Solution

jkr earned 50 total points
ID: 7096340
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

LVL 1

Author Comment

ID: 7098215
wonderful, quick response and exactly the answer as what I need, thx.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month8 days, 19 hours left to enroll