I made a library of time measurement using a clock.
C language standard
compiler
OS
I don't know the rest
__builtin_readcyclecounter ()
for Clang__rdtsc ()
with GCC and x86 architecturetimespec_get ()
I don't know the rest
clockcycle.h
#ifndef CLOCKCYCLE_H
#define CLOCKCYCLE_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifdef __clang__
static inline uint64_t now() {
return __builtin_readcyclecounter();
}
#elif defined(__GNUC__)
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
#include <x86intrin.h>
static inline uint64_t now() {
return __rdtsc();
}
#elif defined(__linux__)
#include <time.h>
static inline uint64_t now() {
struct timespec ts = {0, 0};
timespec_get(&ts, TIME_UTC);
return (uint64_t)(ts.tv_sec) * 1000000000 + ts.tv_nsec;
}
#else
#error unsupported architecture
#endif
#endif
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // CLOCKCYCLE_H
Actually, I would like to rewrite the code of the instruction to read the clock for ARM as well, referring to the following.
https://stackoverflow.com/questions/40454157/is-there-an-equivalent-instruction-to-rdtsc-in-arm/40455065
Recommended Posts