]> www.ginac.de Git - cln.git/blob - include/cln/timing.h
2b650062a3d6442909f308408a9152b00bcd53da
[cln.git] / include / cln / timing.h
1 // Timing tools.
2
3 #ifndef _CL_TIMING_H
4 #define _CL_TIMING_H
5
6 #include "cln/config.h"
7 #include "cln/intparam.h"
8 #include "cln/types.h"
9
10 #include "cln/io.h"
11
12 namespace cln {
13
14 struct cl_timespec {
15         uintL tv_sec;   // seconds since 1970-01-01
16         sintL tv_nsec;  // nanoseconds, >= 0, < 1000000000
17         // Constructors.
18         cl_timespec () {}
19         cl_timespec (uintL sec, sintL nsec)
20                 : tv_sec (sec), tv_nsec (nsec) {}
21 };
22
23 struct cl_time_duration {
24         uintL tv_sec;   // seconds
25         uintL tv_nsec;  // nanoseconds
26         // Constructors.
27         cl_time_duration () {}
28         cl_time_duration (uintL sec)
29                 : tv_sec (sec), tv_nsec (0) {}
30         cl_time_duration (uintL sec, uintL nsec)
31                 : tv_sec (sec), tv_nsec (nsec) {}
32 };
33
34 struct cl_time_consumption {
35         cl_time_duration realtime;      // elapsed time
36         cl_time_duration usertime;      // system's notion of user time/run time
37 };
38
39 extern const cl_time_duration operator- (const cl_timespec&, const cl_timespec&);
40 extern const cl_timespec operator+ (const cl_timespec&, const cl_time_duration&);
41 extern const cl_timespec operator- (const cl_timespec&, const cl_time_duration&);
42 extern const cl_time_duration operator+ (const cl_time_duration&, const cl_time_duration&);
43 extern const cl_time_duration operator- (const cl_time_duration&, const cl_time_duration&);
44
45 extern const cl_timespec cl_current_time ();
46 extern const cl_time_consumption cl_current_time_consumption ();
47
48 // Report a time consumption.
49 // (Should better be a virtual member function of `cl_time_consumption').
50 extern void cl_timing_report (cl_ostream, const cl_time_consumption&);
51
52 struct cl_timing {
53         // Constructor, starts the time interval.
54         cl_timing (cl_time_consumption& accumulator);
55         cl_timing (cl_ostream destination = stderr);
56         cl_timing (const char *, cl_ostream destination = stderr);
57         // Destructor, closes the time interval and does a report.
58         ~cl_timing ();  
59 //private:
60         cl_time_consumption tmp;
61         void (*report_fn) (const cl_timing&);
62         void* report_destination;
63         const char * comment;
64 };
65
66 // Macro for timing.
67 // Usage:
68 //     { CL_TIMING; computation(); }
69 // or  { CL_TIMING(accumulator); computation(); }
70 // or  { CL_TIMING(stdout); computation(); }
71 // The timing interval starts immediately and ends at the closing brace.
72 #define CL_TIMING  CL_TIMING1(__LINE__)
73 #define CL_TIMING1(line)  CL_TIMING2(line)
74 #define CL_TIMING2(line)  cl_timing cl_timing_dummy_##line
75
76 }  // namespace cln
77
78 #endif /* _CL_TIMING_H */