Skip to content

Repository files navigation

TimeThis

Build Status NuGet Version NuGet Downloads Tests C++20 License BSD-3

timethis is a lightweight, header-only Modern C++20 stopwatch utility for measuring code execution time with optional callbacks.


Documentation

siddiqsoft.github.io/timethis


Features

  • RAII-Based Timing: Automatically measures elapsed time from construction to destruction.
  • Optional Callbacks: Execute a function with the elapsed duration on scope exit.
  • std::format Support: Native formatter specialization for std::format.
  • Source Location Tracking: Automatically captures where the timer was created via std::source_location.
  • Stream Output: Direct output stream integration via operator<<.
  • Header-Only C++20: No library linkage required, just include and use.
  • Single Ownership: Deleted copy/move operations prevent accidental misuse.

Quick Example

Basic Timing

#include <iostream>
#include "siddiqsoft/timethis.hpp"

int main()
{
    siddiqsoft::timethis timer;
    // ... do work ...
    std::cout << "Elapsed: " << timer.lap() << " us\n";
    return 0;
}

With Callback

#include <iostream>
#include <chrono>
#include "siddiqsoft/timethis.hpp"

void process()
{
    siddiqsoft::timethis timer([](const auto& duration) {
        auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
        std::cout << "Operation took " << ms.count() << " ms\n";
    });
    // ... do work ...
} // Callback invoked on scope exit

With std::format

#include <iostream>
#include <format>
#include "siddiqsoft/timethis.hpp"

siddiqsoft::timethis timer;
// ... do work ...
std::cout << std::format("{}\n", timer);

Stream Output

siddiqsoft::timethis timer;
// ... do work ...
std::cout << timer << std::endl;  // Outputs: <function_name> took <elapsed>ns

Installation

CMake (CPM)

include(CPM.cmake)
cpmaddpackage("gh:SiddiqSoft/TimeThis#2.5.0")
target_link_libraries(myapp PRIVATE timethis::timethis)

CMake (FetchContent)

include(FetchContent)
FetchContent_Declare(
    timethis
    GIT_REPOSITORY https://github.com/SiddiqSoft/TimeThis.git
    GIT_TAG        v2.5.0
)
FetchContent_MakeAvailable(timethis)
target_link_libraries(myapp PRIVATE timethis::timethis)

NuGet (Windows / Visual Studio)

Install-Package SiddiqSoft.TimeThis

Dependencies

graph TD
    timethis["timethis::timethis"]
    subgraph Test["Test Dependencies (Optional)"]
        GTEST["gtest v1.17.0"]
    end
    timethis -. "timethis_BUILD_TESTS=ON" .-> GTEST
Loading

See dependencies.md for detailed SBOM breakdown.


License

Licensed under the BSD 3-Clause License.

About

Simple stopwatch implementation with optional callback on destructor

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages