A simple C++ header-only profiling tool that outputs events in the Chrome Tracing format. This allows you to measure execution time of functions and code blocks and visualize them with Chrome's built-in timeline viewer.
- Header-only: just one file to include.
- RAII timers: automatically profile functions or scopes.
- Multi-thread aware: logs thread IDs.
- Chrome Tracing output: open results in
chrome://tracing.
#include <profiler/Profiler.hpp>
#include <thread>
#include <vector>
#include <algorithm>
void heavy_function() {
PROFILE_FUNCTION();
std::vector<int> v(1'000'000);
std::iota(v.begin(), v.end(), 0);
std::sort(v.begin(), v.end(), std::greater<int>());
}
int main() {
PROFILE_BEGIN_SESSION("demo", "profile.json");
{
PROFILE_SCOPE("startup");
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
std::thread t1(heavy_function);
std::thread t2(heavy_function);
t1.join();
t2.join();
PROFILE_END_SESSION();
}- Run your program → generates
profile.json. - Open Chrome/Edge and go to
chrome://tracing. - Click Load and select
profile.json. - Explore a timeline of your function calls.
Copy the header into your system include path:
sudo mkdir -p /usr/local/include/profiler
sudo cp include/profiler/Profiler.hpp /usr/local/include/profiler/Now you can include it from anywhere:
#include <profiler/Profiler.hpp>If you prefer CMake:
cmake -S . -B build
cmake --build build
sudo cmake --install buildThis installs the header in /usr/local/include/profiler/.
profiler/
├── include/profiler/Profiler.hpp # Header-only library
├── examples/main.cpp # Usage example
├── docs/screenshots/tracing.png # Screenshot for README
├── CMakeLists.txt # CMake packaging for macOS
├── README.md # This file
└── LICENSE # MIT License
This project is licensed under the MIT License – see the LICENSE file for details.
