Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Created October 12, 2017 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrdhn/ee8f0f9df06973a8c63c30d7ac14f79f to your computer and use it in GitHub Desktop.
Save vrdhn/ee8f0f9df06973a8c63c30d7ac14f79f to your computer and use it in GitHub Desktop.
/**
** Print time taken by various clocks
--- resolutions ---
CLOCK_BOOTTIME : 0.000000001
CLOCK_MONOTONIC : 0.000000001
CLOCK_MONOTONIC_COARSE : 0.003333333
CLOCK_MONOTONIC_RAW : 0.000000001
CLOCK_REALTIME : 0.000000001
CLOCK_REALTIME_COARSE : 0.003333333
--- cpu time for 1000000000 calls ---
CLOCK_BOOTTIME : 97.535666729
CLOCK_MONOTONIC : 22.527615138
CLOCK_MONOTONIC_COARSE : 7.233406602
CLOCK_MONOTONIC_RAW : 92.978536610
CLOCK_REALTIME : 22.894923621
CLOCK_REALTIME_COARSE : 7.026963743
*/
#include <stdio.h>
#include <time.h>
void r(const char *name, int clk_id)
{
struct timespec tp;
clock_getres(clk_id, &tp);
printf("%s: %ld.%.9ld\n", name,tp.tv_sec,tp.tv_nsec);
}
void timespec_diff(struct timespec *start, struct timespec *stop,
struct timespec *result)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
}
void t(const char *name, int clk_id)
{
struct timespec tp;
clock_gettime(clk_id, &tp);
printf("%s: %ld.%.9ld\n", name,tp.tv_sec,tp.tv_nsec);
}
void b(const char *name, int clk_id,long iter)
{
struct timespec start, stop,tp;
clock_gettime(CLOCK_REALTIME, &start);
for( ; iter; iter--) {
clock_gettime(clk_id, &tp);
}
clock_gettime(CLOCK_REALTIME, &stop);
timespec_diff(&start,&stop,&tp);
printf("%s: %ld.%.9ld\n", name,tp.tv_sec,tp.tv_nsec);
}
int main(int argc, char **argv)
{
printf("\n\n--- resolutions --- \n\n");
r("CLOCK_BOOTTIME ", CLOCK_BOOTTIME );
r("CLOCK_MONOTONIC ", CLOCK_MONOTONIC );
r("CLOCK_MONOTONIC_COARSE ", CLOCK_MONOTONIC_COARSE );
r("CLOCK_MONOTONIC_RAW ", CLOCK_MONOTONIC_RAW );
r("CLOCK_REALTIME ", CLOCK_REALTIME );
r("CLOCK_REALTIME_COARSE ", CLOCK_REALTIME_COARSE );
long n = 100L * 100L * 100L * 1000;
printf("\n\n--- cpu time for %ld calls ---\n\n", n);
b("CLOCK_BOOTTIME ", CLOCK_BOOTTIME ,n);
b("CLOCK_MONOTONIC ", CLOCK_MONOTONIC ,n);
b("CLOCK_MONOTONIC_COARSE ", CLOCK_MONOTONIC_COARSE ,n);
b("CLOCK_MONOTONIC_RAW ", CLOCK_MONOTONIC_RAW ,n);
b("CLOCK_REALTIME ", CLOCK_REALTIME ,n);
b("CLOCK_REALTIME_COARSE ", CLOCK_REALTIME_COARSE ,n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment