1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
//
// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#pragma once
#include <chrono>
#include <iostream>
#include <string>
using namespace std::chrono;
namespace common
{
/**
* @brief Used for meausuring performance of specific actions in the code.
* Profiling should be enabled with a parameter passed to the constructor and
* it's disabled by default.
* In order to measure timing, wrap the desired code section with
* ProfilingStart() and ProfilingStopAndPrintUs(title)
*/
class Profiling {
private:
struct group_thousands : std::numpunct<char>
{
std::string do_grouping() const override { return "\3"; }
};
bool mProfilingEnabled{};
steady_clock::time_point mStart{};
steady_clock::time_point mStop{};
public:
Profiling() : mProfilingEnabled(false) {};
/**
* @brief Initializes the profiling object.
*
* * @param[in] isEnabled - Enables the profiling computation and prints.
*/
explicit Profiling(bool isEnabled) : mProfilingEnabled(isEnabled) {};
/**
* @brief Starts the profiling measurement.
*
*/
void ProfilingStart()
{
if (mProfilingEnabled)
{
mStart = steady_clock::now();
}
}
/**
* @brief Stops the profiling measurement, without printing the results.
*
*/
auto ProfilingStop()
{
if (mProfilingEnabled)
{
mStop = steady_clock::now();
}
}
/**
* @brief Get the measurement result in micro-seconds.
*
*/
auto ProfilingGetUs()
{
return mProfilingEnabled ? duration_cast<microseconds>(mStop - mStart).count() : 0;
}
/**
* @brief Stop the profiling measurement and print the result in micro-seconds.
*
*/
void ProfilingStopAndPrintUs(const std::string &title)
{
ProfilingStop();
if (mProfilingEnabled) {
std::cout.imbue(std::locale(std::cout.getloc(), new group_thousands));
std::cout << "Profiling: " << title << ": " << ProfilingGetUs() << " uSeconds" << std::endl;
}
}
};
}// namespace common
|