diff --git a/README b/README new file mode 100644 index 0000000..b6f92cc --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +CPUのサイクル数を取得する関数 + +C言語で記述 diff --git a/cycle_counter.c b/cycle_counter.c new file mode 100644 index 0000000..585609f --- /dev/null +++ b/cycle_counter.c @@ -0,0 +1,26 @@ +#include + +typedef unsigned int uint32_t; +typedef unsigned long int uint64_t; + +// x86_64 +static inline uint64_t cycle_counter(void) { + uint32_t eax, edx; + __asm__ __volatile__("rdtscp" : "=a"(eax), "=d"(edx)::"%ecx"); + return (((uint64_t) edx) << 32) | eax; +} + +int main() +{ + int tmp = 0; + uint64_t start, end; + int i = 0; + + for (i = 0; i < 10; i++) { + start = cycle_counter(); + tmp = 1 + 1; + end =cycle_counter(); + + printf("%ld\n", end - start); + } +}