Last active
May 17, 2023 07:39
-
-
Save leiless/22b6cf392622c6e48ca7ae9d4f15e169 to your computer and use it in GitHub Desktop.
_rdtsc ticks per second estimation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <assert.h> | |
#define N 10 | |
#define SIZE_OF(a) (sizeof(a) / sizeof(*a)) | |
int main(void) { | |
uint64_t arr[N] = {}; | |
uint64_t a, b; | |
unsigned int ret; | |
for (size_t i = 0; i < SIZE_OF(arr); i++) { | |
a = __builtin_ia32_rdtsc(); | |
ret = sleep(1); | |
b = __builtin_ia32_rdtsc(); | |
assert(ret == 0); | |
assert(b >= a); | |
arr[i] = b - a; | |
} | |
uint64_t max = 0; | |
uint64_t min = (uint64_t) -1; | |
uint64_t sum = 0; | |
for (size_t i = 0; i < SIZE_OF(arr); i++) { | |
printf("[%zu] = %zu\n", i, arr[i]); | |
if (max < arr[i]) { | |
max = arr[i]; | |
} | |
if (min > arr[i]) { | |
min = arr[i]; | |
} | |
sum += arr[i]; | |
} | |
printf("\n"); | |
printf("Min = %zu\n", max); | |
printf("Max = %zu\n", min); | |
printf("Max - Min = %zu\n", max - min); | |
printf("Avg = %zu\n", sum / N); | |
return 0; | |
} |
Author
leiless
commented
May 17, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment