PhoenixMicroBenchmark  1.3.0
Toolset for function micro-benchmarking
Loading...
Searching...
No Matches
PFunctionTimer Class Reference

Timer used to time a function call multiple times. More...

#include <PFunctionTimer.h>

Public Member Functions

template<typename _Callable, typename... _Args>
void evalPerf (_Callable &&__f, _Args &&... __args)
 Method which evaluate performance of the given function.
size_t getNbElement () const
 Get the number of elements computed in the performance test.
void getSortedPerf (std::vector< double > &orderPerfNs, std::vector< double > &orderPerfCy) const
 Get the ordered performances.
PFunctionTimeroperator= (const PFunctionTimer &other)
 Definition of equal operator of PFunctionTimer.
 PFunctionTimer (const PFunctionTimer &other)
 Copy constructor of PFunctionTimer.
 PFunctionTimer (size_t nbMeasure=0lu, size_t nbElement=0lu)
 Default constructor of PFunctionTimer.
void resize (size_t nbMeasure)
 Resize the number of possible measures.
bool savePerf (const std::string &fileName) const
 Save the performance of the function in a file.
void setNbElement (size_t nbElement)
 Set the number of elements to consider for the function timing.
virtual ~PFunctionTimer ()
 Destructor of PFunctionTimer.

Protected Member Functions

void copyPFunctionTimer (const PFunctionTimer &other)
 Copy function of PFunctionTimer.

Private Member Functions

void initialisationPFunctionTimer (size_t nbMeasure, size_t nbElement)
 Initialisation function of the class PFunctionTimer.
void saveQuantilePerf (std::ofstream &fs, const std::vector< double > &orderPerfNs, const std::vector< double > &orderPerfCy, const std::string &name, size_t valueIndex) const
 Save the performance of the given quantile.

Private Attributes

size_t p_nbElement
 Number of elements the function is computing (supposed to be constant for all the performance tests).
size_t p_nbMeasure
 Number of performance measures we want on this function.
std::vector< double > p_vecEllapsedTimeCycle
 Ellapsed time of the function in cycles.
std::vector< double > p_vecEllapsedTimeNs
 Ellapsed time of the function in nanoseconds.

Detailed Description

Timer used to time a function call multiple times.

Definition at line 15 of file PFunctionTimer.h.

Constructor & Destructor Documentation

◆ PFunctionTimer() [1/2]

PFunctionTimer::PFunctionTimer ( size_t nbMeasure = 0lu,
size_t nbElement = 0lu )

Default constructor of PFunctionTimer.

Parameters
nbMeasure: number of performance measures we want on this function
nbElement: number of elements the function is computing (supposed to be constant for all the performance tests)

Definition at line 17 of file PFunctionTimer.cpp.

17 {
18 initialisationPFunctionTimer(nbMeasure, nbElement);
19}
void initialisationPFunctionTimer(size_t nbMeasure, size_t nbElement)
Initialisation function of the class PFunctionTimer.

References initialisationPFunctionTimer().

Referenced by copyPFunctionTimer(), operator=(), and PFunctionTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PFunctionTimer() [2/2]

PFunctionTimer::PFunctionTimer ( const PFunctionTimer & other)

Copy constructor of PFunctionTimer.

Parameters
other: class to copy

Definition at line 24 of file PFunctionTimer.cpp.

24 {
25 copyPFunctionTimer(other);
26}
void copyPFunctionTimer(const PFunctionTimer &other)
Copy function of PFunctionTimer.

References copyPFunctionTimer(), and PFunctionTimer().

Here is the call graph for this function:

◆ ~PFunctionTimer()

PFunctionTimer::~PFunctionTimer ( )
virtual

Destructor of PFunctionTimer.

Definition at line 29 of file PFunctionTimer.cpp.

29 {
30
31}

Member Function Documentation

◆ copyPFunctionTimer()

void PFunctionTimer::copyPFunctionTimer ( const PFunctionTimer & other)
protected

Copy function of PFunctionTimer.

Parameters
other: class to copy

Definition at line 142 of file PFunctionTimer.cpp.

142 {
143 p_nbMeasure = other.p_nbMeasure;
144 p_nbElement = other.p_nbElement;
147}
size_t p_nbElement
Number of elements the function is computing (supposed to be constant for all the performance tests).
std::vector< double > p_vecEllapsedTimeNs
Ellapsed time of the function in nanoseconds.
size_t p_nbMeasure
Number of performance measures we want on this function.
std::vector< double > p_vecEllapsedTimeCycle
Ellapsed time of the function in cycles.

References p_nbElement, p_nbMeasure, p_vecEllapsedTimeCycle, p_vecEllapsedTimeNs, and PFunctionTimer().

Referenced by operator=(), and PFunctionTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evalPerf()

template<typename _Callable, typename... _Args>
void PFunctionTimer::evalPerf ( _Callable && __f,
_Args &&... __args )

Method which evaluate performance of the given function.

Parameters
__f: function to be evaluated
__args: arguments of the function The function will be called nbMeasure times (set by the resize method)

Definition at line 33 of file PFunctionTimer_impl.h.

33 {
35 int dummy = 0;
37 for(size_t i(0lu); i < p_nbMeasure; ++i){
38 //Start chrono
39 HiPeTime startNs = phoenix_getTime();
40 size_t startCy = phoenix_rdtsc();
41 //Call function
43 __f(__args...);
44 //Compute ellapsed time
45 size_t ellapsedTimeCy(phoenix_rdtsc() - startCy);
46 p_vecEllapsedTimeNs[i] = (double)ellapsedTimeCy;
47 NanoSecs elapsedTimeNs(phoenix_getTime() - startNs);
48 double fullNs((double)elapsedTimeNs.count());
49 p_vecEllapsedTimeCycle[i] = fullNs;
50 }
51}
void benchmark_doNotOptimize(T &value)
Prevent compiler from optimising a loop.
HiPeTime phoenix_getTime()
Get the current time.
std::chrono::nanoseconds NanoSecs
std::chrono::time_point< std::chrono::steady_clock > HiPeTime
long unsigned int phoenix_rdtsc(void)
#define PIN_THREAD_TO_CORE

References benchmark_doNotOptimize(), p_nbMeasure, p_vecEllapsedTimeCycle, p_vecEllapsedTimeNs, phoenix_getTime(), phoenix_rdtsc(), and PIN_THREAD_TO_CORE.

Here is the call graph for this function:

◆ getNbElement()

size_t PFunctionTimer::getNbElement ( ) const

Get the number of elements computed in the performance test.

Returns
number of elements computed in the performance test

Definition at line 135 of file PFunctionTimer.cpp.

135 {
136 return p_nbElement;
137}

References p_nbElement.

◆ getSortedPerf()

void PFunctionTimer::getSortedPerf ( std::vector< double > & orderPerfNs,
std::vector< double > & orderPerfCy ) const

Get the ordered performances.

Parameters
[out]orderPerfNs: sorted performance in nanoseconds
[out]orderPerfCy: sorted performance in cycles

Definition at line 125 of file PFunctionTimer.cpp.

125 {
126 orderPerfNs = p_vecEllapsedTimeNs;
127 orderPerfCy = p_vecEllapsedTimeCycle;
128 std::sort(orderPerfNs.begin(), orderPerfNs.end());
129 std::sort(orderPerfCy.begin(), orderPerfCy.end());
130}

References p_vecEllapsedTimeCycle, and p_vecEllapsedTimeNs.

Referenced by savePerf().

Here is the caller graph for this function:

◆ initialisationPFunctionTimer()

void PFunctionTimer::initialisationPFunctionTimer ( size_t nbMeasure,
size_t nbElement )
private

Initialisation function of the class PFunctionTimer.

Parameters
nbMeasure: number of performance measures we want on this function
nbElement: number of elements the function is computing (supposed to be constant for all the performance tests)

Definition at line 153 of file PFunctionTimer.cpp.

153 {
154 setNbElement(nbElement);
155 resize(nbMeasure);
156}
void resize(size_t nbMeasure)
Resize the number of possible measures.
void setNbElement(size_t nbElement)
Set the number of elements to consider for the function timing.

References resize(), and setNbElement().

Referenced by PFunctionTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

PFunctionTimer & PFunctionTimer::operator= ( const PFunctionTimer & other)

Definition of equal operator of PFunctionTimer.

Parameters
other: class to copy
Returns
copied class

Definition at line 37 of file PFunctionTimer.cpp.

37 {
38 copyPFunctionTimer(other);
39 return *this;
40}

References copyPFunctionTimer(), and PFunctionTimer().

Here is the call graph for this function:

◆ resize()

void PFunctionTimer::resize ( size_t nbMeasure)

Resize the number of possible measures.

Parameters
nbMeasure: number of performance measures we want on this function

Definition at line 52 of file PFunctionTimer.cpp.

52 {
53 p_nbMeasure = nbMeasure;
56}

References p_nbMeasure, p_vecEllapsedTimeCycle, and p_vecEllapsedTimeNs.

Referenced by initialisationPFunctionTimer().

Here is the caller graph for this function:

◆ savePerf()

bool PFunctionTimer::savePerf ( const std::string & fileName) const

Save the performance of the function in a file.

Parameters
fileName: name of the file to be saved with our function performances
Returns
true on success, false otherwise

Definition at line 78 of file PFunctionTimer.cpp.

78 {
79 if(p_vecEllapsedTimeCycle.size() == 0lu || p_vecEllapsedTimeNs.size() == 0lu){
80 std::cerr << "PFunctionTimer::savePerf : cannot save empty vector in file '"<<fileName<<"'" << std::endl;
81 return false;
82 }
83 std::ofstream fs;
84 fs.open(fileName);
85 if(!fs.is_open()){
86 std::cerr << "PFunctionTimer::savePerf : cannot save file '"<<fileName<<"'" << std::endl;
87 return false;
88 }
89 fs << "#Global performances" << std::endl;
90 fs << "[Performance]" << std::endl;
91 fs << "nb_element = " << p_nbElement << std::endl;
92 fs << "nb_measure = " << p_nbMeasure << std::endl;
93 fs << std::endl;
94 std::vector<double> orderPerfNs, orderPerfCy;
95 getSortedPerf(orderPerfNs, orderPerfCy);
96
97 //Let's save the best performances
98 saveQuantilePerf(fs, orderPerfNs, orderPerfCy, "best", 0lu);
99 //Let's save the median performances
100 saveQuantilePerf(fs, orderPerfNs, orderPerfCy, "median", p_nbMeasure/2lu);
101
102 fs << std::endl;
103 fs << "#Detailed performances" << std::endl;
104 fs << "[Detail]" << std::endl;
105 phoenix_vectorToToml(fs, p_vecEllapsedTimeNs, "measure_nanosecond", 1.0);
106 phoenix_vectorToToml(fs, p_vecEllapsedTimeCycle, "measure_cycles", 1.0);
107 phoenix_vectorToToml(fs, p_vecEllapsedTimeNs, "measure_nanosecond_per_element", (double)p_nbElement);
108 phoenix_vectorToToml(fs, p_vecEllapsedTimeCycle, "measure_cycles_per_element", (double)p_nbElement);
109 //Let's save the ratio of cycles over nanoseconds
110 std::vector<double> vecCyPerNs(p_vecEllapsedTimeCycle.size());
111 std::transform(p_vecEllapsedTimeCycle.begin(), p_vecEllapsedTimeCycle.end(),
112 p_vecEllapsedTimeNs.begin(), vecCyPerNs.begin(),
113 [](double cy, double ns){
114 return cy/ns;
115 });
116 phoenix_vectorToToml(fs, vecCyPerNs, "cycles_over_nanosecond", 1.0);
117 fs.close();
118 return true;
119}
void phoenix_vectorToToml(std::ofstream &fs, const std::vector< double > &vecValue, const std::string &tableName, double nbElement)
Save a vector of values into a toml stream.
void saveQuantilePerf(std::ofstream &fs, const std::vector< double > &orderPerfNs, const std::vector< double > &orderPerfCy, const std::string &name, size_t valueIndex) const
Save the performance of the given quantile.
void getSortedPerf(std::vector< double > &orderPerfNs, std::vector< double > &orderPerfCy) const
Get the ordered performances.

References getSortedPerf(), p_nbElement, p_nbMeasure, p_vecEllapsedTimeCycle, p_vecEllapsedTimeNs, phoenix_vectorToToml(), and saveQuantilePerf().

Here is the call graph for this function:

◆ saveQuantilePerf()

void PFunctionTimer::saveQuantilePerf ( std::ofstream & fs,
const std::vector< double > & orderPerfNs,
const std::vector< double > & orderPerfCy,
const std::string & name,
size_t valueIndex ) const
private

Save the performance of the given quantile.

Parameters
[out]fs: stream to be updated
orderPerfNs: ordered performances in nanoseconds
orderPerfCy: ordered performances in cycles
name: name of the variable
valueIndex: index of the value in the tables

Definition at line 165 of file PFunctionTimer.cpp.

167{
168 fs << "#"<<name<<" performance in nano seconds" << std::endl;
169 fs << name << "_nanosecond = " << orderPerfNs[valueIndex] << std::endl;
170 fs << "#Best performance in cycles" << std::endl;
171 fs << name << "_cycle = " << orderPerfCy[valueIndex] << std::endl;
172 fs << std::endl;
173 fs << "#"<<name<<" performance in nano seconds per elements" << std::endl;
174 fs << name << "_nanosecond_per_element = " << orderPerfNs[valueIndex]/p_nbElement << std::endl;
175 fs << "#"<<name<<" performance in cycles per elements" << std::endl;
176 fs << name << "_cycle_per_element = " << orderPerfCy[valueIndex]/p_nbElement << std::endl;
177 fs << "#"<<name<<" performance in Giga Elements per second" << std::endl;
178 fs << name << "_giga_element_per_second = " << p_nbElement/orderPerfNs[valueIndex] << std::endl;
179 fs << std::endl;
180}

References p_nbElement.

Referenced by savePerf().

Here is the caller graph for this function:

◆ setNbElement()

void PFunctionTimer::setNbElement ( size_t nbElement)

Set the number of elements to consider for the function timing.

Parameters
nbElement: number of elements the function is computing (supposed to be constant for all the performance tests)

Definition at line 45 of file PFunctionTimer.cpp.

45 {
46 p_nbElement = nbElement;
47}

References p_nbElement.

Referenced by initialisationPFunctionTimer().

Here is the caller graph for this function:

Member Data Documentation

◆ p_nbElement

size_t PFunctionTimer::p_nbElement
private

Number of elements the function is computing (supposed to be constant for all the performance tests).

Definition at line 44 of file PFunctionTimer.h.

Referenced by copyPFunctionTimer(), getNbElement(), savePerf(), saveQuantilePerf(), and setNbElement().

◆ p_nbMeasure

size_t PFunctionTimer::p_nbMeasure
private

Number of performance measures we want on this function.

Definition at line 41 of file PFunctionTimer.h.

Referenced by copyPFunctionTimer(), evalPerf(), resize(), and savePerf().

◆ p_vecEllapsedTimeCycle

std::vector<double> PFunctionTimer::p_vecEllapsedTimeCycle
private

Ellapsed time of the function in cycles.

Definition at line 48 of file PFunctionTimer.h.

Referenced by copyPFunctionTimer(), evalPerf(), getSortedPerf(), resize(), and savePerf().

◆ p_vecEllapsedTimeNs

std::vector<double> PFunctionTimer::p_vecEllapsedTimeNs
private

Ellapsed time of the function in nanoseconds.

Definition at line 46 of file PFunctionTimer.h.

Referenced by copyPFunctionTimer(), evalPerf(), getSortedPerf(), resize(), and savePerf().


The documentation for this class was generated from the following files:
  • /builds/CTA-LAPP/PHOENIX_LIBS2/test-benchmark/PhoenixMicroBenchmark/src/PFunctionTimer.h
  • /builds/CTA-LAPP/PHOENIX_LIBS2/test-benchmark/PhoenixMicroBenchmark/src/PFunctionTimer.cpp
  • /builds/CTA-LAPP/PHOENIX_LIBS2/test-benchmark/PhoenixMicroBenchmark/src/PFunctionTimer_impl.h