Skip to content

Instantly share code, notes, and snippets.

@ydnax
Created April 27, 2012 15:03
Show Gist options
  • Save ydnax/2509985 to your computer and use it in GitHub Desktop.
Save ydnax/2509985 to your computer and use it in GitHub Desktop.
small function timer in c++11
#include <iostream>
#include <chrono>
#include <thread>
#include "timer.hpp"
int main(){
std::chrono::milliseconds nap( 2000 );
std::cout<<timer([&](){std::this_thread::sleep_for( nap );}).count()<<std::endl;
}
#include <chrono>
template<class fn>
std::chrono::milliseconds timer(fn f){
auto t1 = std::chrono::high_resolution_clock::now();
f();
auto t2 = std::chrono::high_resolution_clock::now();
return duration_cast<std::chrono::milliseconds>(t2-t1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment