Giter Club home page Giter Club logo

thread-timer's Introduction

thread-timer

Two simple timer class, with a thread safe one !

Content

This little header only file contain 2 classes of timer.

  • The Timer class : compute an interval of time
  • The Thread Timer : a thread safe timer, used to compute the cumulative time spent in a partion of code, in all threads.

How to

The classes has 4 methods :

  • start() : start the timer
  • storp() : stop the timer
  • ms() : return the time recorded between start and stop
  • reset() : reset the timer

The thread timer is a bit more tricky to use : Compared to the "basic" timer, the thread timer can be used INSIDE threads to compute the cumulative time spent in a portion of code (for exemple, in a parallèle for loop). To use it : * declare the thread timer as a global variable * when started, the timer return a token * feed this token to the stop method * enjoy the result of your benchmark ! * * The timer can be set back to ZERO thanks to the reboot() function

Exemple

#include "timer.h"
#include "thread"
#include "iostream"

ThreadTimer myTimer;

int doStuff(int myInt) {
    int token = myTimer.start();
    // do things
    myTimer.stop(token);
    return myInt;
}

int main(int argc, char **argv) {

    std::thread thread1(doStuff, 1);
    std::thread thread2(doStuff, 2);

    thread1.join();
    thread2.join();
    
    std::cout << "Cumulative time in threads : " << myTimer.ms() << endl; 

}

Thanks

Thanks to https://github.com/bshoshany/thread-pool for the original template I completed

thread-timer's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.