you can use sys/timeb.h's timeb struct. If sys/timeb.h is unavailable, you can use Windows.h's SYSTEMTIME. Just replace "struct timeb" with "SYSTEMTIME" and "ftime" with "GetSystemTime". Then replace ".time" with ".wMinute" and ".millitm" with ".wMilliseconds"
Code:
#include <stdio.h>
#include <sys/timeb.h>
int main() {
struct timeb time_start, time_current;
ftime(&time_start);
SomeFunction();
ftime(&time_current);
printf("This program took %d milliseconds to run", time_current.millitm - time_start.millitm);
return 0;
}
But watch out, if you're at, lets say 1:30:59.980 (1:30 PM, 59 seconds, and 980 milliseconds) and your execution takes 30 miliseconds, the difference would be a negative (in this case, -950).
To counter this, an easy yet lazy way is to add 1000 for each second which passes.
Example:
Code:
/* calculates the difference in seconds */
int seconds_difference = time_current.time - time_start.time;
/* if 0 seconds pass between time_start and time_current, there will be no change to time_current.millitm, otherwise 1000 gets added per second to counter time difference */
int milliseconds_difference = (time_current.millitm + (1000 * seconds_difference)) - time_start.millitm;
Therefore, if your program took 15 seconds and 800 milliseconds from 1:30:59.980, the math would be:
Code:
(15 * 1000 + 780) - 980
15780 - 980
14800
Which is the correct amount of time (in milliseconds) from the last time to now.