This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStopWatch.h
51 lines (43 loc) · 2.25 KB
/
StopWatch.h
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
//
// Created by Filippo Valle on 03/10/2018.
//
#ifndef STOPWATCH_H
#define STOPWATCH_H
#if !defined(__CINT__) || defined(__MAKECINT__)
#include <iostream>
#include <chrono>
#endif
#if defined(_OPENMP) // per fare calcolo parallelo
#include <omp.h> // Open MUltiprocessing
#endif
class StopWatch {
public: // quando creo lo StopWatch faccio partire tutti i tempi, quando distruggo lo StopWath, fermo tutti i tempi e li stampo
inline StopWatch(){ // implemento tutto qua perché tanto chiamerò l'oggetto StopWatch solo una volta, quindi conviene implementarlo subito, credo
fSystemTime = std::chrono::system_clock::now(); // tempo REALE
fCPUTime = std::clock(); // tempo del clock della CPU
#if defined(_OPENMP)
fomptime = omp_get_wtime(); //anche questo misura i tempi, nel caso stia usando calcolo parallelo (quindi nel caso abbia chiamato omp
#endif
};
inline ~StopWatch(){
std::cout<<"\nRunned in: "<<GetWall()<<" sec [Wall time]"<<std::endl;
std::cout<<"Runned in: "<<GetCPU()<<" sec [CPU time]"<<std::endl;
#if defined(_OPENMP)
std::cout<<"Runned in: "<<GetOMP()<<" sec [OMP time]"<<std::endl;
#endif
}
protected:
inline double GetWall() const {return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - fSystemTime).count()/1000.;};
// tempo finale reale (che converto in millisecondi) - tempo iniziale reale/1000 (sono microsecondi)
inline double GetCPU() const {return (std::clock() - fCPUTime) / (double)CLOCKS_PER_SEC;} // tempo finale CPU - tempo iniziale CPU : questo mi dà i clock della CPU, poi trasformo in secondi
#if defined(_OPENMP)
inline double GetOMP() const {return (omp_get_wtime() - fomptime);}
#endif
private:
std::chrono::time_point<std::chrono::system_clock> fSystemTime; // variabile del tipo system_clock
std::clock_t fCPUTime; // variabile del tipo clock_t
#if defined(_OPENMP)
double fomptime;
#endif
};
#endif //STOPWATCH_H