Skip to content

golxzn/os-chrono

Repository files navigation

⏱️ golxzn::os::chrono ⏱️

Windows Ubuntu MacOS

Description

The time utilities for golxzn::os.

golxzn::os::chrono contains of:

Each clock and timer has a template argument BaseClock which has to have static method now() returning time_point. You could provide your own base clock to make clocks and timers work with your time point type. But ensure that your time point type has enough resolution to measure time. It has to be at least std::micro.

Dependencies

This library requires golxzn::os::aliases and C++17 compatible compiler.

Usage

golxzh::os::chrono::clock examples

#include <iostream>
#include <golxzn/os/chrono.hpp>

int main() {
	static const auto test_clock = [] (auto clock) {
		size_t counter{};
		while (counter != 1'000'000'000) ++counter;
		return clock.elapsed();
	};

	using namespace golxzn::os::chrono;

	const auto fast_elapsed{     test_clock(fast_clock{}) };
	const auto fast_sys_elapsed{ test_clock(fast_clock<std::chrono::system_clock>{}) };
	const auto just_elapsed{     test_clock(clock{}) };
	const auto just_sys_elapsed{ test_clock(clock<std::chrono::system_clock>{}) };

	std::cout << "Fast elapsed: " << fast_elapsed.milliseconds() << " milliseconds\n";
	std::cout << "Just elapsed: " << just_elapsed.milliseconds() << " milliseconds\n";
	std::cout << "Fast elapsed (sys): " << fast_sys_elapsed.milliseconds() << " milliseconds\n";
	std::cout << "Just elapsed (sys): " << just_sys_elapsed.milliseconds() << " milliseconds\n";
}

golxzh::os::chrono::timer examples

#include <iostream>
#include <golxzn/os/chrono.hpp>

int main() {
	using namespace std::chrono_literals;

	std::cout << "[" << std::this_thread::get_id() << "]: Starting timer for 2 sec\n";
	golxzn::chrono::timer timer{ 1s, [] {
		std::cout << "[" << std::this_thread::get_id() << "]: Timer done\n";
	} };

	while (timer.is_running()) {
		std::cout << "[" << std::this_thread::get_id() << "]: Time left: " << timer.time_left().milliseconds() << " milliseconds\n";
		std::this_thread::sleep_for(100ms);
	}

	std::cout << "[" << std::this_thread::get_id() << "] Exiting\n";

}

Thanks

Documentations is powered by:

Releases

No releases published

Packages

No packages published