-
Notifications
You must be signed in to change notification settings - Fork 1.1k
bench: replace wall-clock timer with per-process CPU timer #1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,25 +17,38 @@ | |
|
|
||
| #include <stdint.h> | ||
|
|
||
| #if (defined(_MSC_VER) && _MSC_VER >= 1900) | ||
| # include <time.h> | ||
| #else | ||
| # include <sys/time.h> | ||
| #if defined(_WIN32) | ||
| # include <windows.h> | ||
| #else /* POSIX */ | ||
| # include <time.h> | ||
| #endif | ||
|
|
||
| static int64_t gettime_i64(void) { | ||
| #if (defined(_MSC_VER) && _MSC_VER >= 1900) | ||
| /* C11 way to get wallclock time */ | ||
| struct timespec tv; | ||
| if (!timespec_get(&tv, TIME_UTC)) { | ||
| fputs("timespec_get failed!", stderr); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL; | ||
| #else | ||
| struct timeval tv; | ||
| gettimeofday(&tv, NULL); | ||
| return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL; | ||
| static int64_t gettime_us(void) { | ||
| #if defined(_WIN32) | ||
|
|
||
| LARGE_INTEGER freq, counter; | ||
| QueryPerformanceFrequency(&freq); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, it's not mandatory but recommended. It is redundant to initialize it every time but I found no other solution since we are not in an OOP environment. and using an if statement to initialize it only the first time doesn't seem optimal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Using an Another nit; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
my reasoning Is the same here. It would be one extra branch. and a failure in the clock is not a huge deal. |
||
| QueryPerformanceCounter(&counter); | ||
| return (int64_t)(counter.QuadPart * 1000000 / freq.QuadPart); | ||
|
|
||
| #else /* POSIX */ | ||
|
|
||
| # if defined(CLOCK_PROCESS_CPUTIME_ID) | ||
| /* In theory, CLOCK_PROCESS_CPUTIME_ID is only useful if the process is locked to a core, | ||
| * see `man clock_gettime` on Linux. In practice, modern CPUs have synchronized TSCs which | ||
| * address this issue, see https://docs.amd.com/r/en-US/ug1586-onload-user/Timer-TSC-Stability . */ | ||
| const clockid_t clock_type = CLOCK_PROCESS_CPUTIME_ID; | ||
| # elif defined(CLOCK_MONOTONIC) | ||
| /* fallback to global timer */ | ||
| const clockid_t clock_type = CLOCK_MONOTONIC; | ||
| # else | ||
| /* fallback to wall-clock timer */ | ||
| const clockid_t clock_type = CLOCK_REALTIME; | ||
| # endif | ||
|
|
||
| struct timespec ts; | ||
| clock_gettime(clock_type, &ts); | ||
| return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; | ||
| #endif | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.