Last active
January 2, 2016 00:19
-
-
Save usagi/8222513 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 <chrono> | |
| #include <thread> | |
| #include "print_time.hxx" | |
| int main() | |
| { | |
| print_time | |
| ( []() | |
| { | |
| // target code | |
| std::this_thread::sleep_for(std::chrono::seconds(1)); | |
| } | |
| , "example" | |
| ); | |
| } |
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
| #pragma once | |
| #include <chrono> | |
| #include <string> | |
| #include <iostream> | |
| inline const std::chrono::microseconds time(const std::function<void()>& f) | |
| { | |
| const auto begin = std::chrono::high_resolution_clock::now(); | |
| f(); | |
| return std::chrono::duration_cast<std::chrono::microseconds> | |
| (std::chrono::high_resolution_clock::now() - begin); | |
| } | |
| inline void print_time(const std::function<void()>& f, const std::string& title = "unknown") | |
| { | |
| auto t = time(f); | |
| std::cerr << "time of " << title << ": " << t.count() << " [us]\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment