20 PHTimer::Frequency PHTimer::_frequency = PHTimer::Frequency();
21 const double PHTimer::_twopower32 = pow(2,32);
24 void PHTimer::Frequency::set_cpu_freq(
const char * path)
30 std::ifstream cpuProcFile(path);
31 if ( !cpuProcFile.is_open())
32 throw std::runtime_error(std::string(
"cpu info. unavailable"));
36 std::string searchString(
"cpu MHz");
37 while ((cpuProcFile.rdstate() & std::ios::failbit) == 0 ) {
40 cpuProcFile.getline(readLine, 1024);
41 std::string readLineString(readLine);
42 if (readLineString.find(searchString) != std::string::npos) {
45 size_t semicolonPosition = readLineString.find(
':', 0);
46 if( semicolonPosition == std::string::npos )
throw std::runtime_error(std::string(
"wrong format for cpu info file"));
47 std::string frequencyString(readLineString.substr(semicolonPosition + 1));
51 std::istringstream frequencySstream(frequencyString);
53 frequencySstream >> freqMHz;
54 _frequency = freqMHz*1e6;
62 double PHTimer::get_difference(
const PHTimer::time_struct &t0,
const PHTimer::time_struct &t1 )
64 unsigned long diff_high = t0._high - t1._high;
65 unsigned long diff_low;
66 if (t0._low < t1._low) {
68 diff_low = (UINT_MAX - t1._low) + t0._low + 1;
69 }
else diff_low = t0._low - t1._low;
71 return (_twopower32*diff_high + diff_low)*_frequency.period();
79 os << std::string(max_col,
'-') << std::endl;
82 int fill = max_col - message.size() - 2;
83 int pre =
static_cast<int>(std::floor(fill/2.0));
84 int post = fill - pre;
85 os << std::string(pre,
'-') <<
" ";
87 os << std::string(post,
'-') << std::endl;
static void PRINT(std::ostream &os=std::cout, const std::string &message="")
print a message (formated) to a stream