PhoenixMicroBenchmark  1.0.1
Toolset for function micro-benchmarking
Loading...
Searching...
No Matches
micro_benchmark_ns_impl.h File Reference
#include <cmath>
#include "micro_benchmark_ns.h"
+ Include dependency graph for micro_benchmark_ns_impl.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PHOENIX_MINIMUM_TIME_NS   1000000000.0
 Minimum time for performance test in ns.
 

Functions

template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNs (double &ellapsedTimeNs, double &ellapsedTimeErrorNs, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns.
 
template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNs (double &ellapsedTimeNs, double &ellapsedTimeErrorNs, double &timePerElement, double &timeErrorPerElement, size_t nbElement, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns.
 
template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNsPrint (const std::string &testName, size_t nbElement, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns and print the result.
 
template<typename _Callable, typename... _Args>
void micro_benchmarkNs (double &ellapsedTimeNs, double &ellapsedTimeErrorNs, double &timePerElement, double &timeErrorPerElement, size_t nbTestPerf, size_t nbCallPerTest, size_t nbElement, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns.
 
template<typename _Callable, typename... _Args>
void micro_benchmarkNs (double &ellapsedTimeNs, double &ellapsedTimeErrorNs, size_t nbTestPerf, size_t nbCallPerTest, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns.
 
template<typename _Callable, typename... _Args>
void micro_benchmarkNsPrint (const std::string &testName, size_t nbTestPerf, size_t nbCallPerTest, size_t nbElement, _Callable &&__f, _Args &&... __args)
 Do the micro benchmarking of a given function and gives performance results in ns and print the result.
 
template<class T>
void phoenix_doNotOptimize (T &value)
 Prevent compiler from optimising a loop.
 
template<class T>
void phoenix_doNotOptimize (T const &value)
 Prevent compiler from optimising a loop.
 

Macro Definition Documentation

◆ PHOENIX_MINIMUM_TIME_NS

#define PHOENIX_MINIMUM_TIME_NS   1000000000.0

Minimum time for performance test in ns.

Definition at line 14 of file micro_benchmark_ns_impl.h.

Referenced by micro_benchmarkAutoNs().

Function Documentation

◆ micro_benchmarkAutoNs() [1/2]

template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNs ( double & ellapsedTimeNs,
double & ellapsedTimeErrorNs,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns.

Parameters
[out]ellapsedTimeNs: ellapsed time in ns
[out]ellapsedTimeErrorNs: error on the ellapsed time in ns
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked This function tries to find automatically a relevant performance measurement

Definition at line 81 of file micro_benchmark_ns_impl.h.

81 {
82 size_t nbTestPerf(100lu), nbCallPerTest(10lu);
83 //Let's try with default values
84 micro_benchmarkNs(ellapsedTimeNs, ellapsedTimeErrorNs, nbTestPerf, nbCallPerTest, __f, __args...);
85// while(std::isnan(ellapsedTimeNs)){
86// std::cout << "micro_benchmarkAutoNs : ellapsedTimeNs is NaN ! Adjusting nbTestPerf = " << nbTestPerf << std::endl;
87// nbCallPerTest += 5lu;
88// micro_benchmarkNs(ellapsedTimeNs, ellapsedTimeErrorNs, nbTestPerf, nbCallPerTest, __f, __args...);
89// std::cout << "micro_benchmarkAutoNs = nbTestPerf = "<<nbTestPerf<<", nbCallPerTest" << nbCallPerTest << std::endl;
90// }
91 double fullEllapsedTime(ellapsedTimeNs*((double)nbTestPerf)*((double)nbCallPerTest));
92 //Check if the total time is more than one second
93 while(fullEllapsedTime < PHOENIX_MINIMUM_TIME_NS && nbCallPerTest < 1000000000lu && nbCallPerTest > 0lu){ //Let's try again if the total time is less than one second
94 //If the total time is less than one second, we try to change nbTestPerf and nbCallPerTest to make it about one second
95 double ratioTime((1.3*PHOENIX_MINIMUM_TIME_NS)/fullEllapsedTime);
96 if(ratioTime < 1.2){
97 ratioTime = 1.2;
98 }
99// double ratioSqrt(std::sqrt(ratioTime) + 1.0);
100 //Let's modify the call
101// nbTestPerf *= ratioSqrt;
102// nbCallPerTest *= ratioSqrt;
103
104 nbCallPerTest *= ratioTime;
105
106 //Let's try again
107 micro_benchmarkNs(ellapsedTimeNs, ellapsedTimeErrorNs, nbTestPerf, nbCallPerTest, __f, __args...);
108 fullEllapsedTime = ellapsedTimeNs*((double)nbTestPerf)*((double)nbCallPerTest);
109 //We loop until we have one second of full time measurement
110 }
111 std::cout << "micro_benchmarkAutoNs : nbCallPerTest = " << nbCallPerTest << std::endl;
112 if(nbCallPerTest > 1000000000lu || nbCallPerTest == 0lu){
113 std::cout << "micro_benchmarkAutoNs : Warning invalid number of calls per test should lead to unrelevant results!!!" << std::endl;
114 }
115}
#define PHOENIX_MINIMUM_TIME_NS
Minimum time for performance test in ns.
void micro_benchmarkNs(double &ellapsedTimeNs, double &ellapsedTimeErrorNs, size_t nbTestPerf, size_t nbCallPerTest, _Callable &&__f, _Args &&... __args)
Do the micro benchmarking of a given function and gives performance results in ns.

References micro_benchmarkNs(), and PHOENIX_MINIMUM_TIME_NS.

Referenced by micro_benchmarkAutoNs(), and micro_benchmarkAutoNsPrint().

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

◆ micro_benchmarkAutoNs() [2/2]

template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNs ( double & ellapsedTimeNs,
double & ellapsedTimeErrorNs,
double & timePerElement,
double & timeErrorPerElement,
size_t nbElement,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns.

Parameters
[out]ellapsedTimeNs: ellapsed time in ns
[out]ellapsedTimeErrorNs: error on the ellapsed time in ns
[out]timePerElement: time per element in ns
[out]timeErrorPerElement: error of time per element in ns
nbElement: number of element treated by the function __f
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked

Definition at line 148 of file micro_benchmark_ns_impl.h.

151{
152 micro_benchmarkAutoNs(ellapsedTimeNs, ellapsedTimeErrorNs, __f, __args...);
153 timePerElement = ellapsedTimeNs/((double)nbElement);
154 timeErrorPerElement = ellapsedTimeErrorNs/((double)nbElement);
155}
void micro_benchmarkAutoNs(double &ellapsedTimeNs, double &ellapsedTimeErrorNs, _Callable &&__f, _Args &&... __args)
Do the micro benchmarking of a given function and gives performance results in ns.

References micro_benchmarkAutoNs().

+ Here is the call graph for this function:

◆ micro_benchmarkAutoNsPrint()

template<typename _Callable, typename... _Args>
void micro_benchmarkAutoNsPrint ( const std::string & testName,
size_t nbElement,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns and print the result.

Parameters
testName: name of the performance test
nbElement: number of element treated by the function __f
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked

Definition at line 181 of file micro_benchmark_ns_impl.h.

181 {
182 double ellapsedTimeNs(0.0), ellapsedTimeErrorNs(0.0), timePerElement(0.0), timeErrorPerElement(0.0);
183 micro_benchmarkAutoNs(ellapsedTimeNs, ellapsedTimeErrorNs, timePerElement, timeErrorPerElement, nbElement, __f, __args...);
184
185 std::cout << testName << " : nbElement = "<<nbElement<<", timePerElement = " << timePerElement << " ns/el ± "<<timeErrorPerElement<<", elapsedTime = " << ellapsedTimeNs << " ns ± "<<ellapsedTimeErrorNs << std::endl;
186 std::cerr << nbElement << "\t" << timePerElement << "\t" << ellapsedTimeNs << "\t" << timeErrorPerElement << "\t" << ellapsedTimeErrorNs << std::endl;
187}

References micro_benchmarkAutoNs().

+ Here is the call graph for this function:

◆ micro_benchmarkNs() [1/2]

template<typename _Callable, typename... _Args>
void micro_benchmarkNs ( double & ellapsedTimeNs,
double & ellapsedTimeErrorNs,
double & timePerElement,
double & timeErrorPerElement,
size_t nbTestPerf,
size_t nbCallPerTest,
size_t nbElement,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns.

Parameters
[out]ellapsedTimeNs: ellapsed time in ns
[out]ellapsedTimeErrorNs: error on the ellapsed time in ns
[out]timePerElement: time per element in ns
[out]timeErrorPerElement: error of time per element in ns
nbTestPerf: number of performance test
nbCallPerTest: number of calls per performance test
nbElement: number of element treated by the function __f
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked

Definition at line 129 of file micro_benchmark_ns_impl.h.

132{
133 micro_benchmarkNs(ellapsedTimeNs, ellapsedTimeErrorNs, nbTestPerf, nbCallPerTest, __f, __args...);
134 timePerElement = ellapsedTimeNs/((double)nbElement);
135 timeErrorPerElement = ellapsedTimeErrorNs/((double)nbElement);
136}

References micro_benchmarkNs().

+ Here is the call graph for this function:

◆ micro_benchmarkNs() [2/2]

template<typename _Callable, typename... _Args>
void micro_benchmarkNs ( double & ellapsedTimeNs,
double & ellapsedTimeErrorNs,
size_t nbTestPerf,
size_t nbCallPerTest,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns.

Parameters
[out]ellapsedTimeNs: ellapsed time in ns
[out]ellapsedTimeErrorNs: error on the ellapsed time in ns
nbTestPerf: number of performance test
nbCallPerTest: number of calls per performance test
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked

Definition at line 45 of file micro_benchmark_ns_impl.h.

47{
48 VecEllapsedTime vecTimeNs;
49 int res = 0;
51 for(size_t i(0lu); i < nbTestPerf; ++i){
52 //Stating the timer
53 HiPeTime beginTime = phoenix_getTime();
54 for(size_t j(0lu); j < nbCallPerTest; ++j){
56 __f(__args...);
57 }
58 //Get the time of the nbCallPerTest calls
59 NanoSecs elapsedTime(phoenix_getTime() - beginTime);
60 double fullNs(elapsedTime.count()/((double)nbCallPerTest));
61 vecTimeNs.push_back(fullNs);
62 }
63 MapOrderedTime mapOrderTime;
64 micro_benchmarkVecToMap(mapOrderTime, vecTimeNs);
65 size_t nbValueUsed(vecTimeNs.size()*0.7 + 1lu);
66 if(nbValueUsed > vecTimeNs.size()){
67 nbValueUsed = vecTimeNs.size();
68 }
69 micro_benchmarkComputeTime(ellapsedTimeNs, ellapsedTimeErrorNs, mapOrderTime, nbValueUsed);
70}
void micro_benchmarkVecToMap(MapOrderedTime &mapOrderTime, const VecEllapsedTime &vecTime)
Fill the map of ordered time with the vector.
void micro_benchmarkComputeTime(double &ellapsedTimeNs, double &ellapsedTimeErrorNs, const MapOrderedTime &mapOrderTime, size_t nbValueToBeUsed)
Compute the total computing time and associated error with the given data.
std::map< double, size_t > MapOrderedTime
Map of the ordered ellapsed time.
std::vector< double > VecEllapsedTime
Vector of ellapsed time measurement.
HiPeTime phoenix_getTime()
Get the current time.
std::chrono::nanoseconds NanoSecs
std::chrono::time_point< std::chrono::steady_clock > HiPeTime
void phoenix_doNotOptimize(T const &value)
Prevent compiler from optimising a loop.

References micro_benchmarkComputeTime(), micro_benchmarkVecToMap(), phoenix_doNotOptimize(), and phoenix_getTime().

Referenced by micro_benchmarkAutoNs(), micro_benchmarkNs(), and micro_benchmarkNsPrint().

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

◆ micro_benchmarkNsPrint()

template<typename _Callable, typename... _Args>
void micro_benchmarkNsPrint ( const std::string & testName,
size_t nbTestPerf,
size_t nbCallPerTest,
size_t nbElement,
_Callable && __f,
_Args &&... __args )

Do the micro benchmarking of a given function and gives performance results in ns and print the result.

Parameters
testName: name of the performance test
nbTestPerf: number of performance test
nbCallPerTest: number of calls per performance test
nbElement: number of element treated by the function __f
__f: function to be called and benchmarked
__args: parameter of the function to be benchmarked

Definition at line 166 of file micro_benchmark_ns_impl.h.

166 {
167 double ellapsedTimeNs(0.0), ellapsedTimeErrorNs(0.0), timePerElement(0.0), timeErrorPerElement(0.0);
168 micro_benchmarkNs(ellapsedTimeNs, ellapsedTimeErrorNs, timePerElement, timeErrorPerElement, nbTestPerf, nbCallPerTest, nbElement, __f, __args...);
169
170 std::cout << testName << " : nbElement = "<<nbElement<<", timePerElement = " << timePerElement << " ns/el ± "<<timeErrorPerElement<<", elapsedTime = " << ellapsedTimeNs << " ns ± "<<ellapsedTimeErrorNs << std::endl;
171 std::cerr << nbElement << "\t" << timePerElement << "\t" << ellapsedTimeNs << "\t" << timeErrorPerElement << "\t" << ellapsedTimeErrorNs << std::endl;
172}

References micro_benchmarkNs().

+ Here is the call graph for this function:

◆ phoenix_doNotOptimize() [1/2]

template<class T>
void phoenix_doNotOptimize ( T & value)
inline

Prevent compiler from optimising a loop.

Parameters
value: reference to a value

Definition at line 28 of file micro_benchmark_ns_impl.h.

28 {
29#if defined(__clang__)
30 asm volatile("" : "+r,m"(value) : : "memory");
31#else
32 asm volatile("" : "+m,r"(value) : : "memory");
33#endif
34}

◆ phoenix_doNotOptimize() [2/2]

template<class T>
void phoenix_doNotOptimize ( T const & value)
inline

Prevent compiler from optimising a loop.

Parameters
value: reference to a value

Definition at line 20 of file micro_benchmark_ns_impl.h.

20 {
21 asm volatile("" : : "r,m"(value) : "memory");
22}

Referenced by micro_benchmarkNs().

+ Here is the caller graph for this function: