dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Stats.cpp
1
6#include "Stats.h"
7
8#include <fmt/core.h>
9
10#include <chrono>
11#include <iostream>
12#include <utility>
13
14#define DLINEAR_STATS_FMT "{:<35} @ {:<26} = {:>15} sec"
15#define DLINEAR_ITERATION_STATS_FMT "{:<35} @ {:<26} = {:>15}"
16
17namespace dlinear {
18
19Stats::Stats(const bool enabled, std::string class_name, std::string operations_name)
20 : timer_{}, enabled_{enabled}, class_name_{std::move(class_name)}, operations_name_{std::move(operations_name)} {}
21
22std::string Stats::ToSegmentString() const {
23 return fmt::format(DLINEAR_STATS_FMT, operations_name_, class_name_, timer_.seconds());
24}
25std::string Stats::ToString() const { return Stats::ToSegmentString(); }
26
27Stats &Stats::operator+=(const Stats &other) {
28 if (class_name_.empty() && !other.class_name_.empty()) class_name_ = other.class_name_;
29 if (operations_name_.empty() && !other.operations_name_.empty()) operations_name_ = other.operations_name_;
30 timer_ += other.timer_;
31 return *this;
32}
33Stats Stats::operator+(const Stats &other) const {
34 Stats result{*this};
35 result += other;
36 return result;
37}
38
40 if (enabled_) std::atomic_fetch_add_explicit(&iterations_, 1, std::memory_order_relaxed);
41}
42
44 return fmt::format(DLINEAR_ITERATION_STATS_FMT, iterations_name_, class_name_, iterations_.load());
45}
47
48IterationStats::IterationStats(bool enabled, std::string class_name, std::string operations_name,
49 std::string iterations_name)
50 : Stats(enabled, std::move(class_name), std::move(operations_name)),
51 iterations_{0},
52 iterations_name_{std::move(iterations_name)} {}
54 : Stats(other), iterations_{other.iterations_.load()}, iterations_name_{other.iterations_name_} {}
55
56void IterationStats::operator++() { Increase(); }
57void IterationStats::operator++(int) { Increase(); }
58IterationStats &IterationStats::operator=(const IterationStats &other) {
59 if (this != &other) {
60 Stats::operator=(other);
61 iterations_ = other.iterations_.load();
62 iterations_name_ = other.iterations_name_;
63 }
64 return *this;
65}
66IterationStats &IterationStats::operator+=(const IterationStats &other) {
67 Stats::operator+=(other);
68 if (iterations_name_.empty() && !other.iterations_name_.empty()) iterations_name_ = other.iterations_name_;
69 std::atomic_fetch_add_explicit(&iterations_, other.iterations_.load(), std::memory_order_relaxed);
70 return *this;
71}
72IterationStats IterationStats::operator+(const IterationStats &other) const {
73 IterationStats result{*this};
74 result += other;
75 return result;
76}
77
78std::ostream &operator<<(std::ostream &os, const Stats &stats) { return os << stats.ToString(); }
79std::ostream &operator<<(std::ostream &os, const IterationStats &stats) { return os << stats.ToString(); }
80
81} // namespace dlinear
Dataclass collecting statistics about some operation or process.
Definition Stats.h:71
std::string ToSegmentString() const override
Convert the current state of the object to a formatted string, only including the specific part the S...
Definition Stats.cpp:43
IterationStats(bool enabled, std::string class_name, std::string name_time="Time spent in Operations", std::string iterations_name="Total # of Iterations")
Construct an IterationStats object.
Definition Stats.cpp:48
std::atomic< unsigned int > iterations_
Atomic counter for the total number of iterations.
Definition Stats.h:73
std::string ToString() const override
Convert the current state of the object to a formatted string.
Definition Stats.cpp:46
std::string iterations_name_
Name to give to the iteration operation.
Definition Stats.h:74
void Increase()
Increase the iteration counter by one.
Definition Stats.cpp:39
Dataclass collecting statistics about some operation or process.
Definition Stats.h:23
std::string class_name_
Name of the class running the operation the Stats object is collecting statistics for.
Definition Stats.h:29
Timer timer_
Timer object to measure the cumulative time spent in the operation.
Definition Stats.h:25
Stats(bool enabled, std::string class_name, std::string name_time="Time spent in Operations")
Construct a Stats object.
Definition Stats.cpp:19
virtual std::string ToSegmentString() const
Convert the current state of the object to a formatted string, only including the specific part the S...
Definition Stats.cpp:22
bool enabled_
Flag to enable/disable the collection of statistics.
Definition Stats.h:28
virtual std::string ToString() const
Convert the current state of the object to a formatted string.
Definition Stats.cpp:25
std::string operations_name_
Name of the operation the Stats object is collecting statistics for.
Definition Stats.h:30
std::chrono::duration< double >::rep seconds() const
Get read-only access to the number elapsed seconds of the timer.
Definition Timer.cpp:56
Global namespace for the dlinear library.
STL namespace.