47 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #pragma once
 | |
| #include <chrono>
 | |
| #include <string>
 | |
| 
 | |
| #define W_NAME 49
 | |
| #define W_TIME 17
 | |
| #define W_FTIME 29
 | |
| // Trick to insert preprocessor into strings
 | |
| #define STR_(X) #X
 | |
| #define STR(X) STR_(X)
 | |
| 
 | |
| #define duration_ns(a) std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
 | |
| #define time() std::chrono::high_resolution_clock::now()
 | |
| 
 | |
| namespace asp {
 | |
| 	std::string format_byte_size(uint64_t) noexcept;
 | |
| 	std::string format_time(const uint64_t) noexcept;
 | |
| 	std::string format_time_ns(uint64_t) noexcept;
 | |
| 	void toolbox_unit_test(void) noexcept;
 | |
| 	std::string thousand_sep(const uint64_t&, const char&) noexcept;
 | |
| 	std::string thousand_sep(const uint64_t&) noexcept;
 | |
| 	void print_separator(const char* const) noexcept;
 | |
| 
 | |
| 	template <typename F, typename... Args>
 | |
| 	inline constexpr void measure_time_void(const char* step_name, const F& fnc, Args &&...args) noexcept {
 | |
| #ifndef __DEBUG
 | |
| 		printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
 | |
| #endif
 | |
| 		const auto start = time();
 | |
| 		fnc(std::forward<Args>(args)...);
 | |
| 		const long long timespent = duration_ns(time() - start);
 | |
| 		printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str());
 | |
| 	}
 | |
| 
 | |
| 	template <typename T, typename F, typename... Args>
 | |
| 	inline constexpr T measure_time(const char* step_name, const F& fnc, Args &&...args) noexcept {
 | |
| #ifndef __DEBUG
 | |
| 		printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\r", step_name, "In progress", "In progress");
 | |
| #endif
 | |
| 		const auto start = time();
 | |
| 		const T res = fnc(std::forward<Args>(args)...);
 | |
| 		const long long timespent = duration_ns(time() - start);
 | |
| 		printf("| %-" STR(W_NAME) "s | %" STR(W_TIME) "s | %-" STR(W_FTIME) "s |\n", step_name, thousand_sep(timespent).c_str(), format_time_ns(timespent).c_str());
 | |
| 		return res;
 | |
| 	}
 | |
| };
 |