Clock.cpp 703 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <chrono>
  2. #include <thread>
  3. #include "server/Clock.h"
  4. Clock::Clock() : last(getNanos()), index(0), sum(0), time(0) {
  5. }
  6. u64 Clock::update() {
  7. index = (index + 1) & (length - 1);
  8. u64 current = getNanos();
  9. sum -= time[index];
  10. time[index] = current - last;
  11. sum += time[index];
  12. last = current;
  13. return time[index];
  14. }
  15. u64 Clock::getLength() const {
  16. return length;
  17. }
  18. float Clock::getUpdatesPerSecond() const {
  19. return length * (1000000000.0f / sum);
  20. }
  21. u64 Clock::getNanos() const {
  22. return std::chrono::steady_clock::now().time_since_epoch().count();
  23. }
  24. void Clock::wait(u64 nanos) const {
  25. std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
  26. }