From f919498f8f4e36477d8ea3424f9ecbfae7576340 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:12:23 -0500 Subject: native_clock: Wait for less time in EstimateRDTSCFrequency In my testing, waiting for 200ms provided the same level of precision as the previous implementation when estimating the RDTSC frequency. This significantly improves the yuzu executable launch times since we reduced the wait time from 3 seconds to 200 milliseconds. --- src/common/x64/native_clock.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/common/x64/native_clock.cpp') diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 28f834443..82ee2c8a1 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp @@ -15,26 +15,26 @@ namespace Common { u64 EstimateRDTSCFrequency() { - const auto milli_10 = std::chrono::milliseconds{10}; - // get current time + // Discard the first result measuring the rdtsc. _mm_mfence(); - const u64 tscStart = __rdtsc(); - const auto startTime = std::chrono::steady_clock::now(); - // wait roughly 3 seconds - while (true) { - auto milli = std::chrono::duration_cast( - std::chrono::steady_clock::now() - startTime); - if (milli.count() >= 3000) - break; - std::this_thread::sleep_for(milli_10); - } - const auto endTime = std::chrono::steady_clock::now(); + __rdtsc(); + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + _mm_mfence(); + __rdtsc(); + + // Get the current time. + const auto start_time = std::chrono::steady_clock::now(); + _mm_mfence(); + const u64 tsc_start = __rdtsc(); + // Wait for 200 milliseconds. + std::this_thread::sleep_for(std::chrono::milliseconds{200}); + const auto end_time = std::chrono::steady_clock::now(); _mm_mfence(); - const u64 tscEnd = __rdtsc(); - // calculate difference - const u64 timer_diff = - std::chrono::duration_cast(endTime - startTime).count(); - const u64 tsc_diff = tscEnd - tscStart; + const u64 tsc_end = __rdtsc(); + // Calculate differences. + const u64 timer_diff = static_cast( + std::chrono::duration_cast(end_time - start_time).count()); + const u64 tsc_diff = tsc_end - tsc_start; const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff); return tsc_freq; } -- cgit v1.2.3