使用c/c++代码获取window的物理硬盘的读写速度/s
时间: 2023-12-02 19:05:25 浏览: 324
要获取Windows系统的物理硬盘的读写速度,你可以使用Windows API函数来实现。以下是一个使用C++代码获取硬盘读写速度的示例:
```cpp
#include <windows.h>
#include <iostream>
int main() {
ULARGE_INTEGER lastTotalReadBytes;
ULARGE_INTEGER lastTotalWriteBytes;
FILETIME lastTime;
if (!GetSystemTimes(&lastTime, NULL, NULL)) {
std::cout << "Failed to get system times." << std::endl;
return 1;
}
if (!GetDiskFreeSpaceEx(NULL, NULL, NULL, &lastTotalWriteBytes)) {
std::cout << "Failed to get disk free space." << std::endl;
return 1;
}
while (true) {
ULARGE_INTEGER totalReadBytes;
ULARGE_INTEGER totalWriteBytes;
FILETIME currentTime;
if (!GetSystemTimes(¤tTime, NULL, NULL)) {
std::cout << "Failed to get system times." << std::endl;
return 1;
}
if (!GetDiskFreeSpaceEx(NULL, NULL, NULL, &totalWriteBytes)) {
std::cout << "Failed to get disk free space." << std::endl;
return 1;
}
ULARGE_INTEGER readBytes;
readBytes.QuadPart = ((ULONGLONG)totalWriteBytes.QuadPart - (ULONGLONG)lastTotalWriteBytes.QuadPart);
ULARGE_INTEGER elapsedTime;
elapsedTime.QuadPart = ((ULONGLONG)currentTime.dwLowDateTime - (ULONGLONG)lastTime.dwLowDateTime);
double readSpeed = (double)readBytes.QuadPart / (double)elapsedTime.QuadPart * 10000;
std::cout << "Disk read speed: " << readSpeed << " bytes/s" << std::endl;
lastTotalReadBytes = totalReadBytes;
lastTime = currentTime;
Sleep(1000); // 每秒更新一次读写速度
}
return 0;
}
```
这个示例代码使用了`GetSystemTimes`函数获取系统时间和`GetDiskFreeSpaceEx`函数获取硬盘空闲空间和写入字节数。然后,通过计算两个时间点之间的差异和写入字节数之差,得出硬盘读取速度。
请注意,以上代码只是一个简单示例,仅获取硬盘的写入速度,并在每秒更新一次读取速度。你可以根据自己的需求进行修改和扩展。
阅读全文