Loading...
Searching...
No Matches
Timer.h
// Copyright (C) 2008-2015 Anders Logg, Jan Blechta, Paul T. Kühner and Garth N.
// Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "TimeLogger.h"
#include <chrono>
#include <optional>
#include <stdexcept>
#include <string>
namespace dolfinx::common
{
template <typename T = std::chrono::high_resolution_clock>
class Timer
{
public:
Timer(std::optional<std::string> task = std::nullopt) : _task(std::move(task))
{
}
~Timer()
{
if (_start_time.has_value() and _task.has_value())
{
_acc += T::now() - *_start_time;
TimeLogger::instance().register_timing(*_task, _acc);
}
}
void start()
{
_acc = T::duration::zero();
_start_time = T::now();
}
template <typename Period = std::ratio<1>>
std::chrono::duration<double, Period> elapsed() const
{
if (_start_time.has_value()) // Timer is running
return T::now() - *_start_time + _acc;
else // Timer is stopped
return _acc;
}
template <typename Period = std::ratio<1>>
std::chrono::duration<double, Period> stop()
{
if (_start_time.has_value()) // Timer is running
{
_acc += T::now() - *_start_time;
_start_time = std::nullopt;
}
return _acc;
}
void resume()
{
if (!_start_time.has_value())
_start_time = T::now();
}
void flush()
{
if (_start_time.has_value())
throw std::runtime_error("Timer must be stopped before flushing.");
if (_task.has_value())
{
TimeLogger::instance().register_timing(*_task, _acc);
_task = std::nullopt;
}
}
private:
// Name of task to register in logger
std::optional<std::string> _task;
// Elapsed time offset
T::duration _acc = T::duration::zero();
// Store start time (std::nullopt if timer has been stopped)
std::optional<typename T::time_point> _start_time = T::now();
};
} // namespace dolfinx::common
dolfinx::common::TimeLogger::register_timing
void register_timing(const std::string &task, std::chrono::duration< double, std::ratio< 1 > > wall)
Register timing (for later summary).
Definition TimeLogger.cpp:23
dolfinx::common::TimeLogger::instance
static TimeLogger & instance()
Singleton access.
Definition TimeLogger.cpp:16
dolfinx::common::Timer::stop
std::chrono::duration< double, Period > stop()
Stop timer and return elapsed time.
Definition Timer.h:92
dolfinx::common::Timer::resume
void resume()
Resume a stopped timer.
Definition Timer.h:106
dolfinx::common::Timer::~Timer
~Timer()
Definition Timer.h:56
dolfinx::common::Timer::start
void start()
Reset elapsed time and (re-)start timer.
Definition Timer.h:66
dolfinx::common::Timer::elapsed
std::chrono::duration< double, Period > elapsed() const
Elapsed time since time has been started.
Definition Timer.h:78
dolfinx::common::Timer::Timer
Timer(std::optional< std::string > task=std::nullopt)
Create and start timer.
Definition Timer.h:50
dolfinx::common::Timer::flush
void flush()
Flush timer duration to the logger.
Definition Timer.h:119
dolfinx::common
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8