File: TestTimer.cpp

package info (click to toggle)
dds 2.9.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 56,576 kB
  • sloc: cpp: 17,621; ansic: 385; makefile: 27; xml: 11; sh: 7
file content (163 lines) | stat: -rw-r--r-- 3,656 bytes parent folder | download | duplicates (3)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
   DDS, a bridge double dummy solver.

   Copyright (C) 2006-2014 by Bo Haglund /
   2014-2018 by Bo Haglund & Soren Hein.

   See LICENSE and README.
*/


#include <iostream>
#include <iomanip>

#include "TestTimer.h"

using std::chrono::duration_cast;
using std::chrono::milliseconds;


TestTimer::TestTimer()
{
  TestTimer::reset();
}


TestTimer::~TestTimer()
{
}


void TestTimer::reset()
{
  name = "";
  count = 0;
  userCum = 0;
  userCumOld = 0;
  sysCum = 0;
}


void TestTimer::setname(const string& s)
{
  name = s;
}


void TestTimer::start(const int number)
{
  count += number;
  user0 = Clock::now();
  sys0 = clock();
}


void TestTimer::end()
{
  time_point<Clock> user1 = Clock::now();
  clock_t sys1 = clock();

  chrono::duration<double, milli> d = user1 - user0;
  int tuser = static_cast<int>(1000. * d.count());

  userCum += tuser;
  sysCum += static_cast<int>((1000 * (sys1 - sys0)) /
    static_cast<double>(CLOCKS_PER_SEC));
}


void TestTimer::printRunning(
  const int reached,
  const int divisor)
{
  if (count == 0)
    return;

  cout << setw(8) << reached << " (" <<
    setw(6) << setprecision(1) << right << fixed <<
      100. * reached / 
        static_cast<float>(divisor) << "%)" <<
    setw(15) << right << fixed << setprecision(0) << 
      (userCum - userCumOld) / 1000. << endl;
  
  userCumOld = userCum;
}


void TestTimer::printBasic() const
{
  if (count == 0) 
    return;

  if (name != "")
    cout << setw(19) << left << "Timer name" << ": " << name << "\n";

  cout << setw(19) << left << "Number of calls" << ": " << count << "\n";

  if (userCum == 0)
    cout << setw(19) << left << "User time" << ": " << "zero" << "\n";
  else
  {
    cout << setw(19) << left << "User time/ticks" << ": " <<
      userCum << "\n";
    cout << setw(19) << left << "User per call" << ": " <<
      setprecision(2) << userCum / static_cast<float>(count) << "\n";
  }

  if (sysCum == 0)
    cout << setw(19) << left << "Sys time" << ": " << "zero" << "\n";
  else
  {
    cout << setw(19) << left << "Sys time/ticks" << ": " <<
      sysCum << "\n";
    cout << setw(19) << left << "Sys per call" << ": " <<
      setprecision(2) << sysCum / static_cast<float>(count) << "\n";
    cout << setw(19) << left << "Ratio" << ": " <<
      setprecision(2) << sysCum / static_cast<float>(userCum);
  }
  cout << endl;
}


void TestTimer::printHands() const
{
  if (name != "")
    cout << setw(21) << left << "Timer name" << 
      setw(12) << right << name << "\n";

  cout << setw(21) << left << "Number of hands" << 
    setw(12) << right << count << "\n";

  if (count == 0)
    return;
  
  if (userCum == 0)
    cout << setw(21) << left << "User time (ms)" <<
      setw(12) << right << "zero" << "\n";
  else
  {
    cout << setw(21) << left << "User time (ms)" <<
      setw(12) << right << fixed << 
        setprecision(0) << userCum / 1000. << "\n";
    cout << setw(21) << left << "Avg user time (ms)" <<
      setw(12) << right << fixed << setprecision(2) << userCum / 
        static_cast<float>(1000. * count) << "\n";
  }

  if (sysCum == 0)
    cout << setw(21) << left << "Sys time" << 
      setw(12) << right << "zero" << "\n";
  else
  {
    cout << setw(21) << left << "Sys time (ms)" <<
      setw(12) << right << fixed << setprecision(0) << sysCum << "\n";
    cout << setw(21) << left << "Avg sys time (ms)" <<
      setw(12) << right << fixed << setprecision(2) << sysCum / 
        static_cast<float>(count) << "\n";
    cout << setw(21) << left << "Ratio" << 
      setw(12) << right << fixed << setprecision(2) << 
      1000. * sysCum / static_cast<float>(userCum);
  }
  cout << endl;
}