正文
增加 41 行删除 41 行
123456788 行未改动171819202122232425262728293031323320 行未改动5455565758596061623 行未改动66676869707172737419 行未改动9495969798991001011021031041051061071081091101111121131141151166 行未改动1231241251261271281291301316 行未改动1381391401411421431441451466 行未改动15315415515615715815916016110 行未改动17217317417517617717817918017 行未改动19819920020120220320420520610 行未改动21721821922022122222322422510 行未改动2362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832847 行未改动2922932942952962972982993002 行未改动```cpp// cpu_benchmark_no_warmup.cpp// ¼òµ¥Ö±½ÓµÄCPUÐÔÄܲâÊÔ£¨ÎÞÔ¤ÈÈ£¬²»¿¨¶Ù£©// ±àÒë: g++ -O2 -std=c++11 -pthread cpu_benchmark_no_warmup.cpp -o cpu_benchmark.exe#include <iostream>#include <chrono>#include <vector>8 行未改动#include <atomic>#include <functional>#include <windows.h>// ==================== ÅäÖòÎÊý ====================namespace Config {const int REPEAT_COUNT = 20; // ÖØ¸´´ÎÊýconst int ITERATIONS = 50000000; // µü´ú´ÎÊýconst int MATRIX_SIZE = 256; // ¾ØÕó´óСconst int FLOPS_ITERATIONS = 20000000;}// ==================== CPUÐÅÏ¢ ====================class CPUInfo {public:static int get_core_count() {SYSTEM_INFO sysInfo;20 行未改动return model;}};// ==================== ¼ÆÊ±Æ÷ ====================class Timer {public:void start() { m_start = std::chrono::high_resolution_clock::now(); }void stop() { m_end = std::chrono::high_resolution_clock::now(); }3 行未改动private:std::chrono::high_resolution_clock::time_point m_start, m_end;};// ==================== ²âÊÔÌ×¼þ ====================class CPUBenchmark {private:std::vector<double> m_results;std::mutex m_mutex;19 行未改动double cv = (stddev / mean) * 100.0;std::cout << "\n" << name << ":\n";std::cout << " ƽ¾ù: " << std::fixed << std::setprecision(2) << mean << " ms\n";std::cout << " ×îС: " << sorted.front() << " ms\n";std::cout << " ×î´ó: " << sorted.back() << " ms\n";std::cout << " ±ê×¼²î: " << stddev << " ms\n";std::cout << " ±äÒìϵÊý: " << cv << "%";if (cv < 3.0) std::cout << " ¡ï";std::cout << "\n";}const std::vector<double>& get_results() const { return m_results; }};// ==================== ²âÊÔº¯Êý ====================// 1. ÕûÊýÔËËãdouble test_integer_ops(int iterations) {Timer timer;volatile int result = 0;6 行未改动timer.stop();return timer.elapsed_ms();}// 2. ¸¡µãÔËËãdouble test_float_ops(int iterations) {Timer timer;volatile float result = 1.0f;6 行未改动timer.stop();return timer.elapsed_ms();}// 3. Ë«¾«¶È¸¡µãdouble test_double_ops(int iterations) {Timer timer;volatile double result = 1.0;6 行未改动timer.stop();return timer.elapsed_ms();}// 4. ÄÚ´æ´ø¿ídouble test_memory_bandwidth() {const int size = 20 * 1024 * 1024 / sizeof(int);std::vector<int> data(size);volatile long long sum = 0;10 行未改动timer.stop();return timer.elapsed_ms();}// 5. ¾ØÕó³Ë·¨double test_matrix_multiply() {const int size = Config::MATRIX_SIZE;std::vector<double> A(size * size);std::vector<double> B(size * size);17 行未改动timer.stop();return timer.elapsed_ms();}// 6. ËØÊý¼ÆËãdouble test_prime_calculation() {const int max_num = 500000;std::vector<bool> is_prime(max_num + 1, true);is_prime[0] = is_prime[1] = false;10 行未改动timer.stop();return timer.elapsed_ms();}// 7. ÅÅÐòdouble test_sorting() {const int size = 100000;std::vector<int> data(size);std::random_device rd;10 行未改动timer.stop();return timer.elapsed_ms();}// ==================== Ö÷³ÌÐò ====================int main() {// ÉèÖÿØÖÆÌ¨±àÂëSetConsoleOutputCP(936);std::cout << "\n" << std::string(60, '=') << "\n";std::cout << " CPUÐÔÄܲâÊÔ (ÎÞÔ¤ÈȰæ)\n";std::cout << std::string(60, '=') << "\n";std::cout << "CPU: " << CPUInfo::get_cpu_model() << "\n";std::cout << "ºËÐÄÊý: " << CPUInfo::get_core_count() << "\n";std::cout << "ÖØ¸´´ÎÊý: " << Config::REPEAT_COUNT << "\n";std::cout << std::string(60, '=') << "\n";// ²âÊÔÅäÖÃstruct TestConfig {std::string name;std::function<double()> func;};std::vector<TestConfig> tests = {{"ÕûÊýÔËËã", []() { return test_integer_ops(Config::ITERATIONS); }},{"µ¥¾«¶È¸¡µã", []() { return test_float_ops(Config::FLOPS_ITERATIONS); }},{"Ë«¾«¶È¸¡µã", []() { return test_double_ops(Config::FLOPS_ITERATIONS); }},{"ÄÚ´æ´ø¿í", []() { return test_memory_bandwidth(); }},{"¾ØÕó³Ë·¨", []() { return test_matrix_multiply(); }},{"ËØÊý¼ÆËã", []() { return test_prime_calculation(); }},{"ÅÅÐòËã·¨", []() { return test_sorting(); }}};// ÔËÐÐËùÓвâÊÔfor (auto& test : tests) {CPUBenchmark benchmark;std::cout << "\n²âÊÔ: " << test.name << "\n";std::cout << " ÔËÐÐ " << Config::REPEAT_COUNT << " ´Î...\n";for (int i = 0; i < Config::REPEAT_COUNT; ++i) {double result = test.func();benchmark.add_result(result);// ½ø¶ÈÌõint progress = (i + 1) * 40 / Config::REPEAT_COUNT;std::cout << "\r [" << std::string(progress, '#')<< std::string(40 - progress, ' ')<< "] " << (i+1) << "/" << Config::REPEAT_COUNT7 行未改动benchmark.print_statistics(test.name);}std::cout << "\n" << std::string(60, '=') << "\n";std::cout << "²âÊÔÍê³É!\n";std::cout << std::string(60, '=') << "\n";system("pause");return 0;2 行未改动123456788 行未改动171819202122232425262728293031323320 行未改动5455565758596061623 行未改动66676869707172737419 行未改动9495969798991001011021031041051061071081091101111121131141151166 行未改动1231241251261271281291301316 行未改动1381391401411421431441451466 行未改动15315415515615715815916016110 行未改动17217317417517617717817918017 行未改动19819920020120220320420520610 行未改动21721821922022122222322422510 行未改动2362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832847 行未改动2922932942952962972982993002 行未改动```cpp// cpu_benchmark_no_warmup.cpp// 简单直接的CPU性能测试(无预热,不卡顿)// 编译: g++ -O2 -std=c++11 -pthread cpu_benchmark_no_warmup.cpp -o cpu_benchmark.exe#include <iostream>#include <chrono>#include <vector>8 行未改动#include <atomic>#include <functional>#include <windows.h>// ==================== 配置参数 ====================namespace Config {const int REPEAT_COUNT = 20; // 重复次数const int ITERATIONS = 50000000; // 迭代次数const int MATRIX_SIZE = 256; // 矩阵大小const int FLOPS_ITERATIONS = 20000000;}// ==================== CPU信息 ====================class CPUInfo {public:static int get_core_count() {SYSTEM_INFO sysInfo;20 行未改动return model;}};// ==================== 计时器 ====================class Timer {public:void start() { m_start = std::chrono::high_resolution_clock::now(); }void stop() { m_end = std::chrono::high_resolution_clock::now(); }3 行未改动private:std::chrono::high_resolution_clock::time_point m_start, m_end;};// ==================== 测试套件 ====================class CPUBenchmark {private:std::vector<double> m_results;std::mutex m_mutex;19 行未改动double cv = (stddev / mean) * 100.0;std::cout << "\n" << name << ":\n";std::cout << " 平均: " << std::fixed << std::setprecision(2) << mean << " ms\n";std::cout << " 最小: " << sorted.front() << " ms\n";std::cout << " 最大: " << sorted.back() << " ms\n";std::cout << " 标准差: " << stddev << " ms\n";std::cout << " 变异系数: " << cv << "%";if (cv < 3.0) std::cout << " ★";std::cout << "\n";}const std::vector<double>& get_results() const { return m_results; }};// ==================== 测试函数 ====================// 1. 整数运算double test_integer_ops(int iterations) {Timer timer;volatile int result = 0;6 行未改动timer.stop();return timer.elapsed_ms();}// 2. 浮点运算double test_float_ops(int iterations) {Timer timer;volatile float result = 1.0f;6 行未改动timer.stop();return timer.elapsed_ms();}// 3. 双精度浮点double test_double_ops(int iterations) {Timer timer;volatile double result = 1.0;6 行未改动timer.stop();return timer.elapsed_ms();}// 4. 内存带宽double test_memory_bandwidth() {const int size = 20 * 1024 * 1024 / sizeof(int);std::vector<int> data(size);volatile long long sum = 0;10 行未改动timer.stop();return timer.elapsed_ms();}// 5. 矩阵乘法double test_matrix_multiply() {const int size = Config::MATRIX_SIZE;std::vector<double> A(size * size);std::vector<double> B(size * size);17 行未改动timer.stop();return timer.elapsed_ms();}// 6. 素数计算double test_prime_calculation() {const int max_num = 500000;std::vector<bool> is_prime(max_num + 1, true);is_prime[0] = is_prime[1] = false;10 行未改动timer.stop();return timer.elapsed_ms();}// 7. 排序double test_sorting() {const int size = 100000;std::vector<int> data(size);std::random_device rd;10 行未改动timer.stop();return timer.elapsed_ms();}// ==================== 主程序 ====================int main() {// 设置控制台编码SetConsoleOutputCP(936);std::cout << "\n" << std::string(60, '=') << "\n";std::cout << " CPU性能测试 (无预热版)\n";std::cout << std::string(60, '=') << "\n";std::cout << "CPU: " << CPUInfo::get_cpu_model() << "\n";std::cout << "核心数: " << CPUInfo::get_core_count() << "\n";std::cout << "重复次数: " << Config::REPEAT_COUNT << "\n";std::cout << std::string(60, '=') << "\n";// 测试配置struct TestConfig {std::string name;std::function<double()> func;};std::vector<TestConfig> tests = {{"整数运算", []() { return test_integer_ops(Config::ITERATIONS); }},{"单精度浮点", []() { return test_float_ops(Config::FLOPS_ITERATIONS); }},{"双精度浮点", []() { return test_double_ops(Config::FLOPS_ITERATIONS); }},{"内存带宽", []() { return test_memory_bandwidth(); }},{"矩阵乘法", []() { return test_matrix_multiply(); }},{"素数计算", []() { return test_prime_calculation(); }},{"排序算法", []() { return test_sorting(); }}};// 运行所有测试for (auto& test : tests) {CPUBenchmark benchmark;std::cout << "\n测试: " << test.name << "\n";std::cout << " 运行 " << Config::REPEAT_COUNT << " 次...\n";for (int i = 0; i < Config::REPEAT_COUNT; ++i) {double result = test.func();benchmark.add_result(result);// 进度条int progress = (i + 1) * 40 / Config::REPEAT_COUNT;std::cout << "\r [" << std::string(progress, '#')<< std::string(40 - progress, ' ')<< "] " << (i+1) << "/" << Config::REPEAT_COUNT7 行未改动benchmark.print_statistics(test.name);}std::cout << "\n" << std::string(60, '=') << "\n";std::cout << "测试完成!\n";std::cout << std::string(60, '=') << "\n";system("pause");return 0;2 行未改动