blob: c96a52a1d70f48b26628f0de50b1c1c42f734c1f (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "timer.h"
#include <stdio.h>
#include <thread>
Timer::Timer()
: _time(),
_timespan(0),
_maxTime(0),
_running(false)
{
}
void
Timer::SetMax(double max)
{
_maxTime = max;
}
void
Timer::Start()
{
if (_running)
return;
_running = true;
_time = clock::now();
}
void
Timer::Stop()
{
if (!_running)
return;
_timespan = GetCurrent();
_running = false;
}
void
Timer::Clear()
{
_running = false;
_timespan = 0;
}
double
Timer::GetTimespan()
{
if (_running)
Stop();
return _timespan;
}
double
Timer::GetRemaining()
{
double span = GetTimespan();
return (span < _maxTime) ? _maxTime - span : 0;
}
double
Timer::GetCurrent()
{
if (!_running)
return 0;
using milliseconds = std::chrono::duration<double, std::milli>;
return std::chrono::duration_cast<milliseconds>(time_point(clock::now()) - _time).count();
}
void
Timer::TestClass()
{
Timer test;
printf("*** Start Testing: class Timer ***\n");
printf("set max time to 5 seconds, then sleep for 1...\n");
test.SetMax(5000);
test.Start();
std::this_thread::sleep_for(std::chrono::seconds(1));
test.Stop();
printf("elapsed: %f, left:%f\n",
test.GetTimespan(), test.GetRemaining());
printf("set max time to 1 second, then sleep for 2...\n");
test.SetMax(1000);
test.Start();
std::this_thread::sleep_for(std::chrono::seconds(2));
test.Stop();
printf("elapsed: %f, left:%f\n",
test.GetTimespan(), test.GetRemaining());
printf("*** Finished Testing: class Timer ***\n");
}
|