Skip to content

Instantly share code, notes, and snippets.

@olzhas
Last active May 29, 2018 11:03
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 olzhas/ceeace3bc2903970161028f7cd46d7cd to your computer and use it in GitHub Desktop.
Save olzhas/ceeace3bc2903970161028f7cd46d7cd to your computer and use it in GitHub Desktop.
simple tic and toc
#ifndef TIMER_H
#define TIMER_H
#include <chrono>
#include <thread>
using timer = std::chrono::high_resolution_clock::time_point;
inline void tic(timer &t){
t = std::chrono::high_resolution_clock::now();
}
inline double toc(const timer &t){
return (std::chrono::high_resolution_clock::now() - t).count()*1e-9;
}
inline void tic(timer *t){
*t = std::chrono::high_resolution_clock::now();
}
inline double toc(const timer *t){
return (std::chrono::high_resolution_clock::now() - *t).count()*1e-9;
}
inline void Sleep(int msec)
{
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
}
#endif // TIMER_H
@olzhas
Copy link
Author

olzhas commented Feb 16, 2018

The example usage code

#include <iostream>
#include "timer.hpp"

int main(int argc, char** argv){
    timer t;
    tic(t); // start timer
    std::cout << "test" << std::endl; 
    // some other activity to time
    double elased = toc(t); // measure elapsed time in seconds
    return 0;
}

for details http://en.cppreference.com/w/cpp/chrono

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment