Skip to content

Commit

Permalink
Move Timer class to a specific header, add dbg folder and header bran…
Browse files Browse the repository at this point in the history
…ch, make Timer class based on a singleton pattern for ease of use
  • Loading branch information
otto-link committed Aug 18, 2024
1 parent c4b3f49 commit 824c332
Show file tree
Hide file tree
Showing 33 changed files with 333 additions and 214 deletions.
2 changes: 1 addition & 1 deletion HighMap/include/highmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "highmap/colormaps.hpp"
#include "highmap/convolve.hpp"
#include "highmap/erosion.hpp"
#include "highmap/export.hpp"
#include "highmap/features.hpp"
#include "highmap/filters.hpp"
#include "highmap/functions.hpp"
Expand All @@ -31,7 +32,6 @@
#include "highmap/gradient.hpp"
#include "highmap/heightmap.hpp"
#include "highmap/hydrology.hpp"
#include "highmap/export.hpp"
#include "highmap/kernels.hpp"
#include "highmap/math.hpp"
#include "highmap/morphology.hpp"
Expand Down
108 changes: 0 additions & 108 deletions HighMap/include/highmap/dbg.hpp

This file was deleted.

182 changes: 182 additions & 0 deletions HighMap/include/highmap/dbg/timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
* Public License. The full license is in the file LICENSE, distributed with
* this software. */

/**
* @file dbg.hpp
* @author Otto Link (otto.link.bv@gmail.com)
* @brief Header file containing the implementation of the Recorder and Timer
* classes for high-resolution timing.
* @version 0.1
* @date 2023-04-30
*
* @copyright Copyright (c) 2023
*
*/
#pragma once

#include <chrono>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>

#include "macrologger.h"

namespace hmap
{

/**
* @brief The Recorder class is responsible for recording timing information for
* individual events.
*/
class Recorder
{
public:
/**
* @brief Constructs a new Recorder object.
*
* @param name The name of the event to be recorded.
*/
Recorder(std::string name);

/**
* @brief Outputs the timing data to the console.
*/
void dump();

/**
* @brief Starts the timer for this Recorder instance.
*/
void start();

/**
* @brief Stops the timer for this Recorder instance and updates the total
* elapsed time.
*/
void stop();

private:
std::string name; ///< The name of the event.
int nb_calls = 0; ///< The number of times the event has been recorded.
std::chrono::high_resolution_clock::time_point
t0; ///< The start time of the event.
float total = 0.f; ///< The total time recorded for the event.
};

/**
* @brief The Timer class is a singleton that manages multiple Recorders and
* provides an interface for timing events.
*
* The Timer class allows you to measure the duration of multiple events by
* using start and stop commands. Each event is identified by a unique name. The
* Timer class maintains a collection of these events and their corresponding
* durations. The class is designed as a singleton, meaning only one instance of
* Timer will exist throughout the lifetime of the program.
*
* ### Usage Example:
*
* @code
* #include "timer.hpp"
*
* int main() {
* // Start timing an event named "step 1"
* Timer::Start("step 1");
*
* // Perform some work here...
*
* // Stop timing the event named "step 1"
* Timer::Stop("step 1");
*
* // Start and stop another event
* Timer::Start("step 2");
*
* // Perform more work here...
*
* Timer::Stop("step 2");
*
* // Dump the timing results for all recorded events
* Timer::Dump();
*
* return 0;
* }
* @endcode
*/
class Timer
{
public:
/**
* @brief Gets the singleton instance of the Timer class.
*
* @return Timer& Reference to the singleton instance.
*/
static Timer &get_instance();

/**
* @brief Starts a timer for the specified event name.
*
* @param name The name of the event to start timing.
*/
static void Start(const std::string &name);

/**
* @brief Stops the timer for the specified event name.
*
* @param name The name of the event to stop timing.
*/
static void Stop(const std::string &name);

/**
* @brief Dumps the timing information for all recorded events to the console.
*/
static void Dump();

private:
/**
* @brief Constructs a new Timer object. This constructor is private to
* enforce the singleton pattern.
*
* @param sid An optional identifier for the Timer instance.
*/
Timer(std::string sid = "");

/**
* @brief Destroys the Timer object and dumps all timing data.
*/
~Timer();

/**
* @brief Starts a timer for the specified event name.
*
* @param name The name of the event to start timing.
*/
void start(const std::string &name);

/**
* @brief Stops the timer for the specified event name.
*
* @param name The name of the event to stop timing.
*/
void stop(const std::string &name);

/**
* @brief Dumps the timing information for all recorded events to the console.
*/
void dump();

// Deleting the copy constructor and assignment operator to enforce singleton
// pattern.
Timer(const Timer &) = delete;
Timer &operator=(const Timer &) = delete;

private:
std::string sid; ///< An optional identifier for the Timer instance.
std::map<std::string, Recorder *>
records; ///< A map of event names to their corresponding Recorder
///< objects.
std::list<Recorder>
data; ///< A list of Recorder objects that store timing information.
int current_level = 0; ///< Current nesting level (if applicable).
};

} // namespace hmap
Loading

0 comments on commit 824c332

Please sign in to comment.