Created
April 27, 2012 15:03
-
-
Save ydnax/2509985 to your computer and use it in GitHub Desktop.
small function timer in c++11
This file contains 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 <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; | |
} |
This file contains 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 <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