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?
Asked
Active
Viewed 61 times
0
-
What platform are you using to run your program? – Bill Lynch Mar 30 '15 at 16:43
-
These sites are probably running in a very different hardware and software environments than yours, so I doubt that you will measure the same time as the sites do. – Eugene Sh. Mar 30 '15 at 16:45
2 Answers
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