Program Listing for File Timer.hpp

Return to documentation for file (include/networkit/auxiliary/Timer.hpp)

/*
 * Timer.hpp
 *
 *  Created on: 14.01.2013
 *      Author: Christian Staudt (christian.staudt@kit.edu)
 */

#ifndef NETWORKIT_AUXILIARY_TIMER_HPP_
#define NETWORKIT_AUXILIARY_TIMER_HPP_

#include <chrono>
#include <cstdint>
#include <string>

#include <networkit/auxiliary/Log.hpp>

namespace Aux {

class Timer {
public:
#ifdef __MIC__
    using my_steady_clock = std::chrono::monotonic_clock;
#else
    using my_steady_clock = std::chrono::steady_clock;
#endif // __MIC__

    Timer() = default;

    my_steady_clock::time_point start() noexcept;

    my_steady_clock::time_point stop() noexcept;

    std::chrono::duration<uint64_t, std::milli> elapsed() const noexcept;

    uint64_t elapsedMilliseconds() const noexcept;

    uint64_t elapsedMicroseconds() const noexcept;

    uint64_t elapsedNanoseconds() const noexcept;

    my_steady_clock::time_point startTime() const noexcept;

    my_steady_clock::time_point stopTime() const noexcept;

    std::string elapsedTag() const;

protected:
    bool running{false};
    my_steady_clock::time_point started;
    my_steady_clock::time_point stopped;

    my_steady_clock::time_point stopTimeOrNow() const noexcept;
};

class StartedTimer : public Timer {
public:
    StartedTimer() : Timer() { start(); }
};

class LoggingTimer : public Timer {
public:
    explicit LoggingTimer(std::string_view label = "",
                          Aux::Log::LogLevel level = Aux::Log::LogLevel::DEBUG);
    ~LoggingTimer();

private:
    Aux::Log::LogLevel level;
    std::string label;
};

} /* namespace Aux */

#endif // NETWORKIT_AUXILIARY_TIMER_HPP_