Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language%20in%20C%20Interview%20Questions%20and%20Answers

Question: How do I get the time elapsed between two function calls ?

Answer: The function difftime( ) finds the difference between two times. It calculates the elapsed time in seconds and returns the difference between two times as a double value.

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

main( )
{
int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ;
int s ;
time_t t1, t2 ; // time_t defines the value used for time function

s = sizeof ( a ) / 2 ;
t1 = time ( NULL ) ;
sel_sort ( a, s ) ; // sort array by selection sort
bub_sort ( a, s ) ; // sort array by bubble sort method
t2 = time ( NULL ) ;
printf ( "nThe difference between two function calls is %f", difftime (
t2, t1 ) ) ;
}

In the above program we have called difftime( ) function that returns the time elapsed from t1 to t2.
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook