sisi4s
Loading...
Searching...
No Matches
Time.hpp
Go to the documentation of this file.
1#ifndef TIME_DEFINED
2#define TIME_DEFINED
3
4#include <ctime>
5#include <cstdint>
6#include <ostream>
7#include <iomanip>
8#include <sstream>
9#include <cmath>
10
11namespace sisi4s {
17class Time : protected timespec {
18public:
19 static constexpr int64_t FRACTIONS = 1000000000;
20 static constexpr int FRACTION_DIGITS = 9;
21
22 Time() {
23 tv_sec = 0;
24 tv_nsec = 0;
25 }
26 Time(int64_t seconds, int64_t nanoSeconds) {
27 tv_sec = seconds;
28 tv_nsec = nanoSeconds;
29 }
30 Time(Time const &t)
31 : timespec(t) {}
32
33 int64_t getSeconds() const { return tv_sec; }
34 int64_t getFractions() const { return tv_nsec; }
35 double getFractionalSeconds() const {
36 return tv_sec + static_cast<double>(tv_nsec) / FRACTIONS;
37 }
38
39 Time &operator+=(Time const &t) {
40 tv_nsec += t.tv_nsec;
41 if (tv_nsec < FRACTIONS) {
42 tv_sec += t.tv_sec;
43 } else {
44 tv_sec += t.tv_sec + 1;
45 tv_nsec -= FRACTIONS;
46 }
47 return *this;
48 }
49
50 Time &operator-=(Time const &t) {
51 if (tv_nsec >= t.tv_nsec) {
52 tv_nsec -= t.tv_nsec;
53 tv_sec -= t.tv_sec;
54 } else {
55 tv_nsec += FRACTIONS - t.tv_nsec;
56 tv_sec -= t.tv_sec + 1;
57 }
58 return *this;
59 }
60
62 Time time;
63 clock_gettime(CLOCK_REALTIME, &time);
64 return time;
65 }
66};
67
68inline Time operator+(Time const &t1, Time const &t2) {
69 Time result(t1);
70 result += t2;
71 return result;
72}
73
74inline Time operator-(Time const &t1, Time const &t2) {
75 Time result(t1);
76 result -= t2;
77 return result;
78}
79
80inline Time operator*(const Time &difference, const double factor) {
81 double d(difference.getFractionalSeconds() * factor);
82 return Time(std::floor(d), std::round((d - std::floor(d)) * Time::FRACTIONS));
83}
84
85inline std::ostream &operator<<(std::ostream &stream, Time const &t) {
86 std::stringstream time;
87 time << t.getSeconds() << "." << std::setw(Time::FRACTION_DIGITS)
88 << std::setfill('0') << t.getFractions();
89 return stream << time.str();
90}
91} // namespace sisi4s
92
93#endif
std::ostream & operator<<(std::ostream &s, const FcidumpReader::FcidumpHeader &h)
Definition FcidumpWriter.cxx:25
Definition Time.hpp:17
int64_t getFractions() const
Definition Time.hpp:34
Time()
Definition Time.hpp:22
Time & operator+=(Time const &t)
Definition Time.hpp:39
Time(Time const &t)
Definition Time.hpp:30
static constexpr int64_t FRACTIONS
Definition Time.hpp:19
static constexpr int FRACTION_DIGITS
Definition Time.hpp:20
int64_t getSeconds() const
Definition Time.hpp:33
Time & operator-=(Time const &t)
Definition Time.hpp:50
static Time getCurrentRealTime()
Definition Time.hpp:61
double getFractionalSeconds() const
Definition Time.hpp:35
Time(int64_t seconds, int64_t nanoSeconds)
Definition Time.hpp:26
Definition Algorithm.hpp:10
FockVector< F > operator+(const FockVector< F > &a, const FockVector< F > &b)
Returns the sum of two FockVectors a and b, where neither a nor b are modified.
Definition FockVector.hpp:406
std::string operator*(const std::string &s, const sisi4s::Permutation< N > &pi)
Definition CcsdPerturbativeTriples.cxx:24
FockVector< F > operator-(const FockVector< F > &a, const FockVector< F > &b)
Returns the difference between two FockVectors a and b, where neither a nor b are modified.
Definition FockVector.hpp:435