foobar2000 SDK  2015-01-14
timers.cpp
Go to the documentation of this file.
1 #include "pfc.h"
2 
3 namespace pfc {
4 
5 #ifdef _WINDOWS
6 
7 profiler_static::profiler_static(const char * p_name)
8 {
9  name = p_name;
10  total_time = 0;
11  num_called = 0;
12 }
13 
15 {
16  try {
18  message << "profiler: " << pfc::format_pad_left<pfc::string_fixed_t<127> >(48,' ',name) << " - " <<
20 
21  if (num_called > 0) {
22  message << " (executed " << num_called << " times, " << (total_time / num_called) << " average)";
23  }
24  message << "\n";
25  OutputDebugStringA(message);
26  } catch(...) {
27  //should never happen
28  OutputDebugString(_T("unexpected profiler failure\n"));
29  }
30 }
31 
32 #else
33 
34  void hires_timer::start() {
35  m_start = nixGetTime();
36  }
37  double hires_timer::query() const {
38  return nixGetTime() - m_start;
39  }
40  double hires_timer::query_reset() {
41  double t = nixGetTime();
42  double r = t - m_start;
43  m_start = t;
44  return r;
45  }
46  pfc::string8 hires_timer::queryString(unsigned precision) {
47  return format_time_ex( query(), precision ).get_ptr();
48  }
49 #endif
50 
51 
52  uint64_t fileTimeWtoU(uint64_t ft) {
53  return (ft - 116444736000000000 + /*rounding*/10000000/2) / 10000000;
54  }
55  uint64_t fileTimeUtoW(uint64_t ft) {
56  return (ft * 10000000) + 116444736000000000;
57  }
58 #ifndef _WIN32
59  uint64_t fileTimeUtoW(const timespec & ts) {
60  uint64_t ft = (uint64_t)ts.tv_sec * 10000000 + (uint64_t)ts.tv_nsec / 100;
61  return ft + 116444736000000000;
62  }
63 #endif
64 
65  uint64_t fileTimeNow() {
66 #ifdef _WIN32
67  uint64_t ret;
68  GetSystemTimeAsFileTime((FILETIME*)&ret);
69  return ret;
70 #else
71  return fileTimeUtoW(time(NULL));
72 #endif
73  }
74 
75 }
const char * name
Definition: timers.h:14
pfc::string8 queryString(unsigned precision=6)
Definition: timers.h:52
double query() const
Definition: timers.h:43
double query_reset()
Definition: timers.h:46
uint64_t fileTimeUtoW(uint64_t ft)
Definition: timers.cpp:55
double nixGetTime()
const char * get_ptr() const
Definition: string_base.h:381
t_uint64 m_start
Definition: timers.h:69
t_uint64 total_time
Definition: timers.h:15
uint64_t fileTimeNow()
Definition: timers.cpp:65
void start()
Definition: timers.h:40
t_uint64 num_called
Definition: timers.h:15
uint64_t fileTimeWtoU(uint64_t ft)
Definition: timers.cpp:52
profiler_static(const char *p_name)
Definition: timers.cpp:7