gpt4 book ai didi

windows - 如何将调用 QueryPerformanceFrequency 的代码移植到 Rust?

转载 作者:行者123 更新时间:2023-12-03 11:42:00 26 4
gpt4 key购买 nike

我需要将此 C 代码移植到 Rust 中:

QueryPerformanceFrequency((unsigned long long int *) &frequency);

我没有找到这样做的功能。

Linux 变体如下所示:

struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0)
frequency = 1000000000;

我应该调用 std::time::Instant::now()并将频率设置为 1000000000?

这是完整的功能:

// Initializes hi-resolution MONOTONIC timer
static void InitTimer(void)
{
srand(time(NULL)); // Initialize random seed

#if defined(_WIN32)
QueryPerformanceFrequency((unsigned long long int *) &frequency);
#endif

#if defined(__linux__)
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0)
frequency = 1000000000;
#endif

#if defined(__APPLE__)
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
frequency = (timebase.denom*1e9)/timebase.numer;
#endif

baseTime = GetTimeCount(); // Get MONOTONIC clock time offset
startTime = GetCurrentTime(); // Get current time
}

最佳答案

访问 Windows API 的直接解决方案是使用 winapi箱。在这种情况下,请调用 QueryPerformanceFrequency :

use std::mem;
use winapi::um::profileapi::QueryPerformanceFrequency;

fn freq() -> u64 {
unsafe {
let mut freq = mem::zeroed();
QueryPerformanceFrequency(&mut freq);
*freq.QuadPart() as u64
}
}

fn main() {
println!("Hello, world!");
}
[dependencies]
winapi = { version = "0.3.8", features = ["profileapi"] }

hi-resolution MONOTONIC timer



我会使用 Instant 作为一个单调的计时器,并假设它的精度足够高,直到证明不是这样。

关于windows - 如何将调用 QueryPerformanceFrequency 的代码移植到 Rust?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60851597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com