Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cross-platform timer example in C.
#include <stdio.h>
#include <time.h>
#define PRINT_INTERVAL 1000 /* in milliseconds */
#define MILLI 0
#define MICRO 1
#define NANO 2
#define RESOLUTION MILLI
long diff_nano(struct timespec *start, struct timespec *end)
{
/* ns */
return ((end->tv_sec * (1000000000)) + (end->tv_nsec)) -
((start->tv_sec * 1000000000) + (start->tv_nsec));
}
long diff_micro(struct timespec *start, struct timespec *end)
{
/* us */
return ((end->tv_sec * (1000000)) + (end->tv_nsec / 1000)) -
((start->tv_sec * 1000000) + (start->tv_nsec / 1000));
}
long diff_milli(struct timespec *start, struct timespec *end)
{
/* ms */
return ((end->tv_sec * 1000) + (end->tv_nsec / 1000000)) -
((start->tv_sec * 1000) + (start->tv_nsec / 1000000));
}
int main(int argc, char **argv)
{
struct timespec start, now, print_timer;
long t, pt;
clock_gettime(CLOCK_MONOTONIC, &print_timer);
clock_gettime(CLOCK_MONOTONIC, &start);
while (1) {
clock_gettime(CLOCK_MONOTONIC, &now);
switch (RESOLUTION) {
case NANO:
t = diff_nano(&start, &now);
break;
case MICRO:
t = diff_micro(&start, &now);
break;
case MILLI:
default:
t = diff_milli(&start, &now);
break;
}
pt = diff_milli(&print_timer, &now);
if (pt >= PRINT_INTERVAL) {
clock_gettime(CLOCK_MONOTONIC, &print_timer);
printf("%ld", t);
switch (RESOLUTION) {
case NANO:
printf(" ns\n");
break;
case MICRO:
printf(" us\n");
break;
case MILLI:
default:
printf(" ms\n");
break;
}
}
clock_gettime(CLOCK_MONOTONIC, &start);
}
return 0;
}
#include <Windows.h>
#include <stdio.h>
long diff_micro(LARGE_INTEGER *start, LARGE_INTEGER *end)
{
LARGE_INTEGER Frequency, elapsed;
QueryPerformanceFrequency(&Frequency);
elapsed.QuadPart = end->QuadPart - start->QuadPart;
//
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second. We use these values
// to convert to the number of elapsed microseconds.
// To guard against loss-of-precision, we convert
// to microseconds *before* dividing by ticks-per-second.
//
elapsed.QuadPart *= 1000000;
elapsed.QuadPart /= Frequency.QuadPart;
return elapsed.QuadPart;
}
long diff_milli(LARGE_INTEGER *start, LARGE_INTEGER *end)
{
LARGE_INTEGER Frequency, elapsed;
QueryPerformanceFrequency(&Frequency);
elapsed.QuadPart = end->QuadPart - start->QuadPart;
//
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second. We use these values
// to convert to the number of elapsed microseconds.
// To guard against loss-of-precision, we convert
// to milliseconds *before* dividing by ticks-per-second.
//
elapsed.QuadPart *= 1000;
elapsed.QuadPart /= Frequency.QuadPart;
return elapsed.QuadPart;
}
int main(int argc, char **argv)
{
LARGE_INTEGER StartingTime, EndingTime;
QueryPerformanceCounter(&StartingTime);
// Activity to be timed
Sleep(1000);
QueryPerformanceCounter(&EndingTime);
printf("%ld us\n", diff_micro(&StartingTime, &EndingTime));
printf("%ld ms\n", diff_milli(&StartingTime, &EndingTime));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment