Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active January 2, 2016 00:19
Show Gist options
  • Select an option

  • Save usagi/8222513 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/8222513 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <thread>
#include "print_time.hxx"
int main()
{
print_time
( []()
{
// target code
std::this_thread::sleep_for(std::chrono::seconds(1));
}
, "example"
);
}
#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