Skip to content

Instantly share code, notes, and snippets.

@ymmt2005
Created May 30, 2013 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymmt2005/5677087 to your computer and use it in GitHub Desktop.
Save ymmt2005/5677087 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <typeinfo>
#include <cxxabi.h>
#include <cstdlib>
#include <string>
#include <iostream>
typedef std::chrono::microseconds us_t;
template <typename T>
inline us_t::rep to_us(T start, T end) {
return std::chrono::duration_cast<us_t>(end-start).count();
}
class demangler {
public:
demangler(const char* name) {
int status;
char* demangled = abi::__cxa_demangle(name, 0, 0, &status);
if( status == 0 ) {
m_name = demangled;
std::free(demangled);
} else {
m_name = name;
}
}
const std::string& name() const {
return m_name;
}
private:
std::string m_name;
};
template <typename T>
inline void print_ratio() {
demangler t(typeid(T).name());
std::cout << t.name() << " ratio = "
<< T::period::num << "/" << T::period::den
<< " second." << std::endl;
}
int main() {
auto t1 = std::chrono::steady_clock::now();
print_ratio<std::chrono::system_clock>();
print_ratio<std::chrono::steady_clock>();
print_ratio<std::chrono::high_resolution_clock>();
std::cout << "std::chrono::high_resolution_clock is "
<< (std::chrono::high_resolution_clock::is_steady ? "" : "not ")
<< "steady." << std::endl;
static_assert(std::chrono::steady_clock::is_steady,
"std::chrono::steady_clock is not steady!");
auto t2 = std::chrono::steady_clock::now();
std::cout << "t2 - t1 = "
<< to_us(t1, t2) << "us"
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment