Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / BilatTools_CPP / src / core / tools / cpp / Chronos.cpp
1 #include <iostream>
2 #include <omp.h>
3 #include <stdio.h>
4 #include <string>
5
6 #include "Chronos.h"
7
8 using std::cout;
9 using std::endl;
10
11 /*----------------------------------------------------------------------*\
12 |* Implementation *|
13 \*---------------------------------------------------------------------*/
14
15 /*--------------------------------------*\
16 |* Constructor *|
17 \*-------------------------------------*/
18
19 Chronos::Chronos()
20 {
21 start();
22 }
23
24 Chronos::~Chronos()
25 {
26 // rien
27 }
28
29 /*--------------------------------------*\
30 |* Methodes *|
31 \*-------------------------------------*/
32
33 void Chronos::start()
34 {
35 // cout << "Chronos : start" << endl;
36
37 isRunning = true;
38
39 deltaTime = -1;
40 timeStart = time();
41 }
42
43 double Chronos::stop()
44 {
45 //cout << "Chronos : stop" << endl;
46
47 isRunning = false;
48
49 timeStop = time();
50 deltaTime = timeStop - timeStart;
51
52 return deltaTime;
53 }
54
55 double Chronos::getDeltaTime() const
56 {
57 if (isRunning)
58 {
59 cout << "Chronos : Warning : Stop chrono befor, or use methode timeFlight() instead!" << endl;
60 return -1;
61 }
62 else
63 {
64 return deltaTime;
65 }
66 }
67
68 double Chronos::timeFlight() const
69 {
70 return time() - timeStart;
71 }
72
73 void Chronos::print(const string& titre) const
74 {
75 print(cout, titre);
76 }
77
78 void Chronos::print(ostream& stream, const string& titre) const
79 {
80 double dt;
81 if (isRunning)
82 {
83 dt = timeFlight();
84 }
85 else
86 {
87 dt = deltaTime;
88 }
89 stream << titre << " " << dt << " (s)" << endl;
90 }
91
92 double Chronos::time()
93 {
94 // return time(NULL); // #include <time.h> time(NULL) est time_t
95 // Problem : Sous linux : compte en seconde!
96
97 return omp_get_wtime(); //#include <omp.h>
98 }
99
100 /*--------------------------------------*\
101 |* Friend *|
102 \*-------------------------------------*/
103
104 ostream& operator <<(ostream& stream, const Chronos& chrono) // friend
105 {
106 chrono.print(stream);
107 return stream;
108 }
109
110 /*----------------------------------------------------------------------*\
111 |* End *|
112 \*---------------------------------------------------------------------*/
113