0

on some competitive programming sites, I came across the time a function is taking. Now I am concerned is there any method to measure the time a function takes in execution in C?

2 Answers2

2
int start = os_call_to_get_time();
func();
int end = os_call_to_get_time();
printf("call took %d secs\n", end  - start);

in real life you probably want to do it in milli- or microseconds. Or execute the function several thousand times

pm100
  • 48,078
  • 23
  • 82
  • 145
0

You should use struct timeval for this purpose.

void funtion()

 {

 struct timeval t1,t2;

 gettimeofday(&t1,Null);

 //Functionality

 gettimeofday(&t2,Null);

 //Print the difference

 }

This code can run on windows and linux

Yasir Majeed
  • 739
  • 3
  • 12