Skip to content

Instantly share code, notes, and snippets.

@inxanedev
Last active August 30, 2020 16:36
Show Gist options
  • Save inxanedev/5ffa50831c549602169a1a9b7d50824f to your computer and use it in GitHub Desktop.
Save inxanedev/5ffa50831c549602169a1a9b7d50824f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <chrono>
double benchmark(void (*f)()) {
auto start = std::chrono::high_resolution_clock::now();
// Call the function
(*f)();
auto end = std::chrono::high_resolution_clock::now();
// Subtract times and return result as a double
std::chrono::duration<double> dur = end - start;
return dur.count();
}
// Function to benchmark
void func() {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
int main() {
double duration = benchmark(func);
std::cout << duration << std::endl;
std::cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment