比毒蘑菇占用更高的CPU性能测试器
2026-07-29 15:25:50
发布于:浙江
AI写的,不喜勿喷
主流x86_64架构的都可以用
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <iomanip>
#include <stdexcept>
#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#endif
unsigned int get_hardware_concurrency() {
unsigned int n = std::thread::hardware_concurrency();
if (n == 0) {
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
return n;
}
void bind_thread_to_cpu(std::thread& t, int core_id) {
if (!t.joinable()) return;
#ifdef _WIN32
HANDLE handle = reinterpret_cast<HANDLE>(t.native_handle());
DWORD_PTR mask = 1ULL << core_id;
SetThreadAffinityMask(handle, mask);
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_setaffinity_np(t.native_handle(), sizeof(cpu_set_t), &cpuset);
#endif
}
void worker_task(int id, long long& result_iterations) {
auto start = std::chrono::high_resolution_clock::now();
long long count = 0;
while (true) {
for (int i = 0; i < 1000000; ++i) {
// 防止优化
#if defined(__GNUC__) || defined(__clang__)
asm volatile("" ::: "memory");
#elif defined(_MSC_VER)
_ReadWriteBarrier();
#endif
}
count++;
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (duration.count() >= 10000) break;
}
result_iterations = count;
}
int main() {
int n; std::cout << "请输入测试次数:"; std::cin >> n;
long long *cnt = new long long[n], *cnt_number = new long long;
for (int i=0; i<n; i++) cnt[i] = 0; *cnt_number = 0;
for (int i=0; i<n; i++) {
unsigned int num_cores = get_hardware_concurrency();
if (i == 0) std::cout << "========= Detected " << num_cores << " cores =========" << std::endl;
std::vector<std::thread> threads;
std::vector<long long> results(num_cores, 0);
try {
for (unsigned int i = 0; i < num_cores; ++i) {
threads.emplace_back(worker_task, i, std::ref(results[i]));
bind_thread_to_cpu(threads.back(), static_cast<int>(i));
}
for (auto& t : threads) {
if (t.joinable()) t.join();
}
long long run_cnt = 0;
std::cout << "\n--- Results (Iterations in 1 sec) ---" << std::endl;
for (size_t i = 0; i < results.size(); ++i) {
std::cout << "Core " << std::setw(2) << i << ": " << std::setw(10) << results[i] / 10 << " M/s" << std::endl;
run_cnt += results[i];
}
std::cout << "Sum to " << results.size() << " Cores:" << run_cnt / 10 << " M/s " << run_cnt / results.size() / 10 << " M/s" << std::endl;
*cnt_number += run_cnt / results.size();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
Sleep(1000); // 让CPU休息一会,单位ms
std::cout << std::endl;
}
std::cout << *cnt_number / n / 10 << " M/s" << std::endl;
delete cnt_number;
delete[] cnt;
system("pause");
return 0;
}
效果:

可以改的地方有注释,别太狠了。学校电脑能把你崩飞。
由于几乎是空循环,优先级又是默认,因此你不会感觉非常卡顿,但是你会发现别的C++程序输出变成了打字机(迫真)。
全部评论 2
666撞头像了
2026-07-29 来自 四川
1这是我解包Create得到的(.jar是压缩文件)
2026-07-30 来自 浙江
0嗯嗯,知道
2026-07-30 来自 四川
0我是从模组网站拉下来的
2026-07-30 来自 四川
0
编译需要[C++11:)才能编译
2026-07-29 来自 浙江
0





















有帮助,赞一个