Git Product home page Git Product logo

cputimer's Introduction

CpuTime

C++11 header only timer library (clock_gettime wrapper)

CI Contributor Covenant

Allows you to measure

  • Real time spent
  • CPU time spent by this process
  • CPU time spent by this thread

Only tested on Linux and macOS

Example:

You can measure real time with RealTiemr, CPU time spent by this process with ProcessTimer, and CPU time spent by this thread with ThreadTimer. Here's an example with RealTimer, the other two share the same API:

    // Initialising the timer does not start it
    knatten::CpuTimer::RealTimer realTimer;

    // Start the timer
    realTimer.start();

    // Do some work
    doSomeWork();

    // Get the elapsed time, in nanoseconds by default
    std::cout << "Doing some work took " << realTimer.elapsed().count()
              << " nanoseconds\n\n";

You can restart the timer by calling .start() again:

    // Call .start() again to restart the timer
    realTimer.start();

You can specify the chrono type you want the result in. Above, it defaulted to std::chrono::nanoseconds, but you can set it to other types too:

realTimer.elapsed<std::chrono::milliseconds>().count()

You can also measure all three types at once with the Timer class:

    // Initialising the timer does not start it
    knatten::CpuTimer::Timer timer;

    // Start the timer
    timer.start();

    // Do some work
    doSomeWork();

    const auto elapsed = timer.elapsed<std::chrono::milliseconds>();
    std::cout << "Doing some work took:\n  " << elapsed.realTime.count()
              << " milliseconds of real/wall time\n  "
              << elapsed.processTime.count()
              << " milliseconds of process CPU time\n  "
              << elapsed.threadTime.count()
              << " milliseconds of thread CPU time\n";

See also the example

Installing

This is a header only library, you can just copy src/include/CpuTimer.h into your project.

If you prefer Conan, you can clone the repository and do conan create src. You can then use the cputimer Conan package in your project, and e.g. do find_package(cputimer 1.0.0) / target_link_libraries(YourTarget cputimer::cputimer) in CMake.

Building the full project with tests

This is using Conan, if you don't like Conan just have a look at src/conanfile.py and install the dependencies listed in the requirements method manually.

  • pip install ci/requirements.txt (virtualenv recommended)
  • mkdir build && cd build
  • conan install --build=missing -of=. ../src (optionally provide a profile with -pr=<profile>)
  • cmake ../../src/ -DCMAKE_BUILD_TYPE=<build type> (-GNinja recommended)
  • cmake --build .
  • ctest To run the tests

CI

To run CI locally:

  • ./ci/build-and-test.sh <Debug|Relase> builds the code and runs the tests in Debug/Release
  • ./ci/format.sh <--fix|--check> fixes or checks formatting of all C++ code
  • ./ci/clang-tidy.sh builds the code with Clang Tidy
  • ./ci/sanitizer.sh <address|thread|undefined> builds the code and runs the tests with the chosen sanitizer

Conan

To create a conan package:

conan create src

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.