Skip to content

Instantly share code, notes, and snippets.

@mmj-the-fighter
Last active May 16, 2025 15:59
Show Gist options
  • Save mmj-the-fighter/e7fb14151c0f4d987ca00289982135b7 to your computer and use it in GitHub Desktop.
Save mmj-the-fighter/e7fb14151c0f4d987ca00289982135b7 to your computer and use it in GitHub Desktop.
simple profiler which supports nested calls
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org/>
*/
#ifndef _PROFILER_HPP_
#define _PROFILER_HPP_
#include <chrono>
#include <vector>
#include <iostream>
namespace util {
#define CODEPROFILES_RESERVATION_SIZE 100
enum class TimeUnit {
Nanoseconds,
Microseconds,
Milliseconds,
Seconds
};
using Clock = std::chrono::high_resolution_clock;
using TimePoint = Clock::time_point;
using CallStack = std::vector<long>;
struct CodeProfile {
long tag;
TimeUnit unit;
long serial;
double deltaTime;
TimePoint startTime;
CodeProfile(
long aTag,
TimeUnit aUnit,
long aSerial,
double aDeltaTime
)
:
tag(aTag),
unit(aUnit),
serial(aSerial),
deltaTime(aDeltaTime)
{
startTime = Clock::now();
}
};
using ProfilerDb = std::vector<CodeProfile>;
class Profiler{
public:
inline static Profiler& GetInstance() {
static Profiler instance;
return instance;
}
inline void SetSecondsAsTimeUnit() {
preferredUnit = TimeUnit::Seconds;
}
inline void SetMillisAsTimeUnit() {
preferredUnit = TimeUnit::Milliseconds;
}
inline void SetMicrosAsTimeUnit() {
preferredUnit = TimeUnit::Microseconds;
}
inline void SetNanosAsTimeUnit() {
preferredUnit = TimeUnit::Nanoseconds;
}
inline void ClearAndReset(){
serialNumber = 0;
db.clear();
stk.clear();
}
inline void Begin(long tag){
snippetTag = tag;
db.emplace_back(
CodeProfile(
snippetTag,
preferredUnit,
serialNumber,
0.0
)
);
stk.push_back(serialNumber);
++serialNumber;
}
inline void End(){
long serial = stk.back();
stk.pop_back();
TimePoint startTime = db[serial].startTime;
TimePoint endTime = Clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::nanoseconds>
(endTime - startTime);
double unitDivisor = 1.0;
switch (preferredUnit) {
case TimeUnit::Microseconds:
unitDivisor = 1e3;
break;
case TimeUnit::Milliseconds:
unitDivisor = 1e6;
break;
case TimeUnit::Seconds:
unitDivisor = 1e9;
break;
default:
break;
}
db[serial].deltaTime = duration.count() / unitDivisor;
}
inline void Print() {
for (const auto& codeProfile : db) {
std::cout
//<< codeProfile.serial << " "
<< codeProfile.tag << " --- "
<< codeProfile.deltaTime;
switch (codeProfile.unit) {
case TimeUnit::Nanoseconds:
std::cout << " nanoseconds";
break;
case TimeUnit::Microseconds:
std::cout << " microseconds";
break;
case TimeUnit::Milliseconds:
std::cout << " milliseconds";
break;
case TimeUnit::Seconds:
std::cout << " seconds";
break;
default:
std::cout << " unknown unit";
break;
}
std::cout << '\n';
}
}
private:
ProfilerDb db;
CallStack stk;
TimeUnit preferredUnit;
long serialNumber;
long snippetTag;
Profiler() :
preferredUnit(TimeUnit::Milliseconds),
serialNumber(0),
snippetTag(0L)
{
db.reserve(CODEPROFILES_RESERVATION_SIZE);
stk.reserve(CODEPROFILES_RESERVATION_SIZE);
}
Profiler(const Profiler&) = delete;
Profiler& operator=(const Profiler&) = delete;
};
#undef CODEPROFILES_RESERVATION_SIZE
class ProfilerScope
{
public:
ProfilerScope(long tag){
util::Profiler::GetInstance().Begin(tag);
}
~ProfilerScope(){
util::Profiler::GetInstance().End();
}
};
class ProfilerLimitedScope
{
public:
bool profilerStarted;
ProfilerLimitedScope(long num, long limit){
if (num < limit){
util::Profiler::GetInstance().Begin(num);
profilerStarted = true;
}
else{
profilerStarted = false;
}
}
~ProfilerLimitedScope(){
if (profilerStarted){
util::Profiler::GetInstance().End();
}
}
};
}
#endif
#include <iostream>
#include <thread>
#include "profiler.hpp"
void Delay(int ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
void fn2()
{
auto& p = util::Profiler::GetInstance();
p.Begin(21);
Delay(200);
p.End();
p.Begin(22);
Delay(300);
p.End();
}
void fn1()
{
auto& p = util::Profiler::GetInstance();
p.Begin(1);
Delay(100);
fn2();
p.End();
}
int main(){
auto& p = util::Profiler::GetInstance();
p.SetMillisAsTimeUnit();
fn1();
p.Print();
p.ClearAndReset();
p.Begin(3);
Delay(240);
p.End();
p.Print();
}
//output
/*
1 --- 601.839 milliseconds
21 --- 200.932 milliseconds
22 --- 300.193 milliseconds
3 --- 243.583 milliseconds
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment