| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | //! @file | ||
| 3 | /* | ||
| 4 | TTS - Tiny Test System | ||
| 5 | Copyright : TTS Contributors & Maintainers | ||
| 6 | SPDX-License-Identifier: BSL-1.0 | ||
| 7 | */ | ||
| 8 | //====================================================================================================================== | ||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #if defined(_WIN32) | ||
| 12 | #define WIN32_LEAN_AND_MEAN | ||
| 13 | #define NOMINMAX | ||
| 14 | #include <windows.h> | ||
| 15 | #include <profileapi.h> | ||
| 16 | #else | ||
| 17 | #include <time.h> | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #include <tts/tools/text.hpp> | ||
| 21 | #include <tts/tools/units.hpp> | ||
| 22 | |||
| 23 | namespace tts::_ | ||
| 24 | { | ||
| 25 | // Monotonic elapsed-time only, not std::chrono - see doc/compile_time.hpp. | ||
| 26 | 562 | inline nanoseconds now_ns() | |
| 27 | { | ||
| 28 | #if defined(_WIN32) | ||
| 29 | LARGE_INTEGER freq; | ||
| 30 | LARGE_INTEGER count; | ||
| 31 | QueryPerformanceFrequency(&freq); | ||
| 32 | QueryPerformanceCounter(&count); | ||
| 33 | return static_cast<nanoseconds>(static_cast<double>(count.QuadPart) * 1e9 / | ||
| 34 | static_cast<double>(freq.QuadPart)); | ||
| 35 | #else | ||
| 36 | struct timespec ts; | ||
| 37 | 562 | clock_gettime(CLOCK_MONOTONIC, &ts); // NOSONAR - avoiding <chrono>, see comment above | |
| 38 | 562 | return static_cast<nanoseconds>(ts.tv_sec) * 1'000'000'000ULL + | |
| 39 | 562 | static_cast<nanoseconds>(ts.tv_nsec); | |
| 40 | #endif | ||
| 41 | } | ||
| 42 | |||
| 43 | // Thresholds sit just below each power of 1000 so rounding can't show e.g. "1000.000 ms". | ||
| 44 | 478 | inline ::tts::text format_duration(double duration_ns) | |
| 45 | { | ||
| 46 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 450 times.
|
478 | if(duration_ns < 999.5) return ::tts::text {"%.0f ns", duration_ns}; |
| 47 |
2/2✓ Branch 0 taken 439 times.
✓ Branch 1 taken 11 times.
|
450 | if(duration_ns < 999'999.5) return ::tts::text {"%.3f us", duration_ns / 1'000.0}; |
| 48 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if(duration_ns < 999'999'500.0) return ::tts::text {"%.3f ms", duration_ns / 1'000'000.0}; |
| 49 | ✗ | return ::tts::text {"%.3f s", duration_ns / 1'000'000'000.0}; | |
| 50 | } | ||
| 51 | } | ||
| 52 |