-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
93 lines (79 loc) · 2.15 KB
/
Timer.cpp
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
//
// Created by arturo on 10/02/23.
//
#include <iostream>
#include <chrono>
#include <ctime>
#include <cmath>
class Timer
{
public:
void start()
{
m_StartTime = std::chrono::system_clock::now();
m_bRunning = true;
}
void stop()
{
m_EndTime = std::chrono::system_clock::now();
m_bRunning = false;
}
double elapsed()
{
std::chrono::time_point<std::chrono::system_clock> endTime;
if(m_bRunning)
{
endTime = std::chrono::system_clock::now();
}
else
{
endTime = m_EndTime;
}
return std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - m_StartTime).count();
}
double elapsedSeconds()
{
return elapsed() / 1000.0;
}
private:
std::chrono::time_point<std::chrono::system_clock> m_StartTime;
std::chrono::time_point<std::chrono::system_clock> m_EndTime;
bool m_bRunning = false;
};
/*
long fibonacci(unsigned n)
{
if (n < 2) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
// std::chrono::time_point<std::chrono::system_clock> start, end;
// start = std::chrono::system_clock::now();
// Timer timer;
// timer.start();
// std::cout << "f(42) = " << fibonacci(42) << '\n';
// timer.stop();
//
// std::cout << "Time: " << timer.elapsed() << std::endl;
// end = std::chrono::system_clock::now();
// std::chrono::duration<double> elapsed_seconds = end-start;
// std::time_t end_time = std::chrono::system_clock::to_time_t(end);
// std::cout << "finished computation at " << std::ctime(&end_time)
// << "elapsed time: " << elapsed_seconds.count() << "s\n";
Timer timer;
timer.start();
int counter = 0;
double test, test2;
while(timer.elapsedSeconds() < 10.0)
{
counter++;
test = std::cos(counter / M_PI);
test2 = std::sin(counter / M_PI);
}
timer.stop();
std::cout << counter << std::endl;
std::cout << "Seconds: " << timer.elapsedSeconds() << std::endl;
std::cout << "Milliseconds: " << timer.elapsed() << std::endl
;
}*/