Please consider this code :
#include
#include <stdio.h>
#include <unistd.h>
int main()
{
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
ProfiledFunction();
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Elapsed time: %ld milliseconds\n", mtime);
return 0;
}
</unistd.h></stdio.h>
ProfiledFunction
is the function which is being profiled.
The code was extracted from here :
http://stackoverflow.com/questions/588307/c-obtaining-milliseconds-time-on-linux-clock-doesnt-seem-to-work-properl[
^]
If you need microsecond precision just use
tv_usec
part of
timeval
structure.
If you're developing in kernel mode please consider using this function :
http://kerneltrap.org/node/6393[
^]
Hope it helps.