/usr/include/elektra/kdbtimer.hpp is in libelektra-dev 0.8.14-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <string>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <sys/time.h>
#ifdef __GNUC__
#define TIMER_NOINLINE __attribute__((noinline))
#endif
class Timer
{
public:
TIMER_NOINLINE void start()
{
gettimeofday (&begin, 0);
}
TIMER_NOINLINE void stop()
{
gettimeofday (&end, 0);
// force calculation in long long:
timer_t result = end.tv_sec - begin.tv_sec;
result *= usec_factor;
result += end.tv_usec - begin.tv_usec;
results.push_back(result);
}
Timer(std::string const & name);
~Timer(); // print csv table at end
struct timeval begin;
struct timeval end;
typedef long long timer_t;
typedef std::vector<timer_t> results_t;
results_t results;
std::string name;
static const timer_t usec_factor = 1000000LL;
};
inline Timer::Timer(std::string const & name_) :
begin(),
end(),
results(),
name(name_)
{
}
inline Timer::~Timer()
{
for (auto result : results)
{
std::cerr << name << ","
<< result / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< result % Timer::usec_factor
<< std::endl;
}
}
inline std::ostream & operator<< (std::ostream & os, Timer const & t)
{
Timer::timer_t r = t.results.back();
os.width(30);
os.fill(' ');
os << t.name << "\t";
os << r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec";
r = std::accumulate(t.results.begin(), t.results.end(), 0LL);
os << "\t Sum: "
<< r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec";
r = r / t.results.size();
os << "\tAvg: "
<< r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec";
r = *std::min_element(t.results.begin(), t.results.end());
os << "\tMin: "
<< r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec";
r = *std::max_element(t.results.begin(), t.results.end());
os << "\tMax: "
<< r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec";
Timer::results_t md = t.results;
nth_element(md.begin(),
md.begin()+md.size()/2,
md.end());
r=*(md.begin()+md.size()/2);
os << "\tMedian: "
<< r / Timer::usec_factor
<< "."
<< std::setw(6)
<< std::setfill('0')
<< r % Timer::usec_factor
<< " sec"
<< std::endl;
return os;
}
|