:moneybag: A portable CPU profiler with ASCII,CSV,TSV,Markdown,chrome://tracing support (C++11)
#include "dollar.hpp" // dollar is enabled by default. compile with -D$= to disable any profiling
int main() { $ // <-- put a dollar after every curly brace to determinate cpu cost of the scope
for( int x = 0; x < 10000000; ++x ) { $ // <-- functions or loops will apply too
// slow stuff...
}
dollar::text(std::cout); // report stats to std::cout in text format; see also csv(), tsv() and markdown()
dollar::chrome(std::ofstream("chrome.json")); // write tracing results to a json file (for chrome://tracing embedded profiler)
dollar::clear(); // clear all scopes (like when entering a new frame)
}
$
dollar sign in front of it.dollar raii("name")
object.$
macro just adds function name, line and number to the RAII object name.dollar::text(ostream)
, dollar::csv(ostream)
, dollar::tsv(ostream)
or dollar::markdown(ostream)
to print a text report in any ostream object (like std::cout
).dollar::chrome(ostream)
to write a chrome://tracing friendly json trace.dollar::clear()
when entering a new frame.-D$=
to disable it.DOLLAR_USE_OPENMP
to use OpenMP timers (instead of <chrono>
timers)DOLLAR_MAX_TRACES
to change number of maximum instrumented samples (default: 512).DOLLAR_CPUMETER_WIDTH
to change width of CPU meter bars (default: 10 characters).~/dollar> cat ./sample.cc
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include "dollar.hpp"
void x( int counter ) { $
while( counter-- > 0 ) { $
std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
}
}
void c( int counter ) { $
while( counter-- > 0 ) { $
std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
}
}
void y( int counter ) { $
while( counter-- > 0 ) { $
std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
if( counter % 2 ) c(counter); else x(counter);
}
}
void a( int counter ) { $
while( counter-- > 0 ) { $
std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
y(counter);
}
}
int main() { $
a(10);
// write tracing results to a json file (for chrome://tracing embedded profiler)
std::ofstream file("chrome.json");
dollar::chrome(file);
// display results
dollar::text(std::cout);
// clear next frame
dollar::clear();
}
~/dollar> g++ sample.cc -std=c++11 && ./a.out
1. +-main (./dollar.hpp:535) [..........] 0.00% CPU ( 0.000ms) 1 hits
2. +-a (./dollar.hpp:528) [..........] 0.00% CPU ( 0.000ms) 1 hits
3. +-a (./dollar.hpp:529) [..........] 5.71% CPU ( 20.000ms) 10 hits
4. +-y (./dollar.hpp:522) [..........] 0.00% CPU ( 0.000ms) 10 hits
5. +-y (./dollar.hpp:523) [==........] 25.71% CPU ( 90.000ms) 45 hits
6. |-c (./dollar.hpp:517) [..........] 0.00% CPU ( 0.000ms) 20 hits
7. | +-c (./dollar.hpp:518) [===.......] 34.29% CPU ( 120.000ms) 60 hits
8. +-x (./dollar.hpp:512) [..........] 0.00% CPU ( 0.000ms) 25 hits
9. +-x (./dollar.hpp:513) [===.......] 34.29% CPU ( 120.000ms) 60 hits
~/dollar> g++ sample.cc -std=c++11 -D$= && ./a.out
~/dollar>
max()
macro conflict