23 Ağustos 2019 Cuma

std::chrono::high_resolution_clock Sınıfı

Giriş 
Şu satırı dahil ederiz.
#include <chrono>
VS 2012'de bu sınıf system_clock ile aynıdır. Yani sadece typedef'ten ibaret. Açıklaması şöyle. Yani duvar saati değildir, monotonic saat te (yani std::chrono::steady_clock) değildir. Sistem saati diye düşünülebilir.
While the name sounds like it's what you want, there is unfortunately no guarantee whether this clock follows the wall clock or the monotonic clock. This means that if you are unfortunate enough that your NTP daemon is making an adjustment to the wall clock time while your benchmark is running, the results will be incorrect. std::chrono::steady_clock is the best clock from the C++ standard to use to avoid surprises.
now metodu
Şöyle yaparız.
std::chrono::time_point<std::chrono::high_resolution_clock> t_start = 
  std::chrono::high_resolution_clock::now();
Diğer
Örnek
İki time_point arasındaki farkı almak için std::chrono::duration_cast<T> kullanılır. T tipi örneğin std::chrono::nanoseconds veya başka bir tip olabilir. Şöyle yaparız.
auto d = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count();
Örnek
İki time_point arasındaki farkı almak için yardımcı metod olarak şöyle yaparız.
// timing stuff
using namespace std::chrono_literals;
using hrclock = std::chrono::high_resolution_clock;
using time_point = hrclock::time_point;

static double relatime(time_point tp = hrclock::now()) {
  static const time_point t_start = hrclock::now();
  return (tp - t_start)/1.0ms;
}
Örnek
İki time_point arasındaki farkı almak için yardımcı metod olarak elimizde şöyle bir kod olsun.
class timer {
  private:
    decltype(std::chrono::high_resolution_clock::now()) begin, end;

  public:
    void start() {
      begin = std::chrono::high_resolution_clock::now();
    }

    void stop() {
      end = std::chrono::high_resolution_clock::now();
    }

    template<typename T>
    auto duration() const {
         return std::chrono::duration_cast<T>(end - begin).count();
    }

    auto nanoseconds() const {
         return duration<std::chrono::nanoseconds>();
    }

    void printNS() const {
         std::cout << "Nanoseconds: " << nanoseconds() << std::endl;
    }
};
Şöyle yaparız.
timer t1;
t1.start();
...
t1.stop();
t1.printNS();

Hiç yorum yok:

Yorum Gönder