Skip to content

Instantly share code, notes, and snippets.

@esnosy
Created June 30, 2023 22:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esnosy/73e7c534e0f29e0e97049b5bce0543b5 to your computer and use it in GitHub Desktop.
Save esnosy/73e7c534e0f29e0e97049b5bce0543b5 to your computer and use it in GitHub Desktop.
Neat TIMER macro for C++11
# GPLv3 iyadahmed430@gmail.com (Iyad Ahmed)
#pragma once
#include <chrono>
#include <functional>
void time_function(const char *message, std::function<void()> function)
{
auto start = std::chrono::high_resolution_clock::now();
function();
auto end = std::chrono::high_resolution_clock::now();
std::cout << message << " took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms"
<< std::endl;
}
#define TIMER(message, code) time_function(message, [&]() { code });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment