Created
July 26, 2021 02:08
-
-
Save AndrewBelt/73f81fd74c9cc40de3f411b34dd74c9b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <cstdlib> | |
#include <cstdio> | |
#include <cmath> | |
#include <cstring> | |
#include <cstdint> | |
#include <algorithm> | |
#include <time.h> | |
double getTime() { | |
struct timespec ts; | |
clock_gettime(CLOCK_MONOTONIC_RAW, &ts); | |
int64_t time = int64_t(ts.tv_sec) * 1000000000LL + ts.tv_nsec; | |
return time / 1e9; | |
} | |
template <typename F> | |
void benchmark(uint64_t n, F f) { | |
double sum = 0.0; | |
double sum2 = 0.0; | |
double min = INFINITY; | |
double max = 0.0; | |
for (uint64_t j = 0; j < n; j++) { | |
double start = getTime(); | |
f(); | |
double end = getTime(); | |
double t = end - start; | |
sum += t; | |
sum2 += t*t; | |
min = std::min(min, t); | |
max = std::max(max, t); | |
} | |
printf("mean: %8g ", sum / n); | |
printf("stddev: %8g ", std::sqrt((sum2 / n) - std::pow(sum / n, 2))); | |
printf("min: %8g ", min); | |
printf("max: %8g\n", max); | |
} | |
int main() { | |
benchmark(1000000ll, [&]() { | |
// ... | |
}); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment