foobar2000 SDK  2015-08-03
timers.cpp
Go to the documentation of this file.
1 #include "pfc.h"
2 
3 namespace pfc {
4 
5 #ifdef PFC_HAVE_PROFILER
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 #endif
32 
33 #ifndef _WIN32
34 
35  void hires_timer::start() {
36  m_start = nixGetTime();
37  }
38  double hires_timer::query() const {
39  return nixGetTime() - m_start;
40  }
41  double hires_timer::query_reset() {
42  double t = nixGetTime();
43  double r = t - m_start;
44  m_start = t;
45  return r;
46  }
47  pfc::string8 hires_timer::queryString(unsigned precision) {
48  return format_time_ex( query(), precision ).get_ptr();
49  }
50 #endif
51 
52 
53  uint64_t fileTimeWtoU(uint64_t ft) {
54  return (ft - 116444736000000000 + /*rounding*/10000000/2) / 10000000;
55  }
56  uint64_t fileTimeUtoW(uint64_t ft) {
57  return (ft * 10000000) + 116444736000000000;
58  }
59 #ifndef _WIN32
60  uint64_t fileTimeUtoW(const timespec & ts) {
61  uint64_t ft = (uint64_t)ts.tv_sec * 10000000 + (uint64_t)ts.tv_nsec / 100;
62  return ft + 116444736000000000;
63  }
64 #endif
65 
66  uint64_t fileTimeNow() {
67 #ifdef _WIN32
68  uint64_t ret;
69  GetSystemTimeAsFileTime((FILETIME*)&ret);
70  return ret;
71 #else
72  return fileTimeUtoW(time(NULL));
73 #endif
74  }
75 
76 }
const char * name
Definition: timers.h:16
pfc::string8 queryString(unsigned precision=6)
Definition: timers.h:70
const char * get_ptr() const
Definition: string_base.h:492
double query() const
Definition: timers.h:61
double query_reset()
Definition: timers.h:64
uint64_t fileTimeUtoW(uint64_t ft)
Definition: timers.cpp:56
double nixGetTime()
t_uint64 total_time
Definition: timers.h:17
uint64_t fileTimeNow()
Definition: timers.cpp:66
void start()
Definition: timers.h:58
t_uint64 num_called
Definition: timers.h:17
uint64_t fileTimeWtoU(uint64_t ft)
Definition: timers.cpp:53
profiler_static(const char *p_name)
Definition: timers.cpp:7