Skip to content

Commit

Permalink
Merge pull request #157 from eseiler/misc/timer
Browse files Browse the repository at this point in the history
[MISC] Add max and avg to timer
  • Loading branch information
eseiler authored Nov 2, 2023
2 parents 7fa3a81 + 941cc4c commit 84c4aea
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions include/hibf/misc/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class timer
using rep_t =
std::conditional_t<is_concurrent, std::atomic<std::chrono::steady_clock::rep>, std::chrono::steady_clock::rep>;

using count_t = std::conditional_t<is_concurrent, std::atomic<uint64_t>, uint64_t>;

static constexpr int alignment = is_concurrent ? 64 : 8;

template <concurrent concurrency_>
friend class timer;

Expand All @@ -58,14 +62,21 @@ class timer

timer(timer const & other)
requires is_concurrent
: start_{other.start_}, stop_{other.stop_}, ticks{other.ticks.load()}
:
start_{other.start_},
stop_{other.stop_},
ticks{other.ticks.load()},
max{other.max.load()},
count{other.count.load()}
{}
timer & operator=(timer const & other)
requires is_concurrent
{
start_ = other.start_;
stop_ = other.stop_;
ticks = other.ticks.load();
max = other.max.load();
count = other.count.load();
return *this;
}

Expand All @@ -78,13 +89,18 @@ class timer
{
stop_ = std::chrono::steady_clock::now();
assert(stop_ >= start_);
ticks += (stop_ - start_).count();
std::chrono::steady_clock::rep duration = (stop_ - start_).count();
ticks += duration;
update_max(duration);
++count;
}

template <concurrent concurrency_>
void operator+=(timer<concurrency_> const & other)
{
ticks += other.ticks;
update_max(other.ticks);
++count;
}

double in_seconds() const
Expand All @@ -93,12 +109,38 @@ class timer
return std::chrono::duration<double>(std::chrono::steady_clock::duration{ticks.load()}).count();
}

double max_in_seconds() const
requires is_concurrent
{
return std::chrono::duration<double>(std::chrono::steady_clock::duration{max.load()}).count();
}

double avg_in_seconds() const
requires is_concurrent
{
assert(count.load() > 0u);
return in_seconds() / count.load();
}

// GCOVR_EXCL_START
double in_seconds() const
requires (!is_concurrent)
{
return std::chrono::duration<double>(std::chrono::steady_clock::duration{ticks}).count();
}

double max_in_seconds() const
requires (!is_concurrent)
{
return std::chrono::duration<double>(std::chrono::steady_clock::duration{max}).count();
}

double avg_in_seconds() const
requires (!is_concurrent)
{
assert(count > 0u);
return in_seconds() / count;
}
// GCOVR_EXCL_STOP

// Timer are always equal.
Expand All @@ -110,7 +152,26 @@ class timer
private:
std::chrono::steady_clock::time_point start_{std::chrono::time_point<std::chrono::steady_clock>::max()};
std::chrono::steady_clock::time_point stop_{};
rep_t ticks{};

alignas(alignment) rep_t ticks{};
alignas(alignment) rep_t max{};
alignas(alignment) count_t count{};

void update_max(std::chrono::steady_clock::rep const value)
requires is_concurrent
{
for (std::chrono::steady_clock::rep previous_value = max;
previous_value < value && !max.compare_exchange_weak(previous_value, value, std::memory_order_relaxed);)
;
}

// GCOVR_EXCL_START
void update_max(std::chrono::steady_clock::rep const value)
requires (!is_concurrent)
{
max = std::max(max, value);
}
// GCOVR_EXCL_STOP
};

/*!\brief Alias for timer<concurrent::no>
Expand Down

1 comment on commit 84c4aea

@vercel
Copy link

@vercel vercel bot commented on 84c4aea Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hibf – ./

hibf-seqan.vercel.app
hibf-git-main-seqan.vercel.app
hibf.vercel.app

Please sign in to comment.