code-tutorial.com

The best resources for developers

Is gettimeofday() guaranteed to be of microsecond resolution?

11867927879484732
11867927879692217
it took this long to call printf: 207485
#include <stdio.h>
#include <stdint.h>

inline uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (
      "xorl %%eax, %%eax
"
      "cpuid
"
      "rdtsc
"
      : "=a" (lo), "=d" (hi)
      :
      : "%ebx", "%ecx");
    return (uint64_t)hi << 32 | lo;
}

main()
{
    unsigned long long x;
    unsigned long long y;
    x = rdtsc();
    printf("%lld
",x);
    y = rdtsc();
    printf("%lld
",y);
    printf("it took this long to call printf: %lld
",y-x);
}

Tags: unix linux winapi timer visual-c++

Source: By Bernard as answer to the question

This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 4.0


Related code-snippets: