Sha256: 4c4243404795a634eba68e5fbc9d17eeb0769eb915bceaaad5a68fcdc5aa969b

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

/**
 * @file
 * Declares a simple timer class.
 */
#pragma once

#include <chrono>

namespace leatherman { namespace util {

/**
 * A simple stopwatch/timer we can use for user feedback.  We use the
 * std::chrono::steady_clock as we don't want to be affected if the system
 * clock changed around us (think ntp skew/leapseconds).
 */
class Timer {
  public:
    Timer() {
        reset();
    }

    /** @return Returns the time that has passed since last reset in seconds. */
    double elapsed_seconds() {
        auto now = std::chrono::steady_clock::now();
        return std::chrono::duration<double>(now - start_).count();
    }

    /** @return Returns the time that has passed since last reset in milliseconds. */
    int elapsed_milliseconds() {
        auto now = std::chrono::steady_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_).count();
    }

    /** Resets the clock. */
    void reset() {
        start_ = std::chrono::steady_clock::now();
    }

  private:
    std::chrono::time_point<std::chrono::steady_clock> start_;
};

}}  // namespace leatherman::util

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
facter-3.12.2.cfacter.20181217 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.12.1.cfacter.20181031 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.6.cfacter.20181031 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.12.1.cfacter.20181023 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.5.cfacter.20181022 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.12.0.cfacter.20181004 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.12.0.cfacter.20181001 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.12.0.cfacter.20180918 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.4.cfacter.20180821 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.3.cfacter.20180716 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.2.cfacter.20180612 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.9.6.cfacter.20180612 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.2.cfacter.20180606 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.9.6.cfacter.20180606 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
facter-3.11.0.cfacter.20180319 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp
cfacter-3.11.0.rc.20180314 ext/facter/leatherman/util/inc/leatherman/util/timer.hpp