跳到正文

个人记录

DEEPSEEK的人机测试

lxy_qwq约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年6月27日
收录
2026年6月27日
更新
2026年11月11日
篇幅
约 17 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
// 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>
#include <cmath>
#include <random>
#include <thread>
#include <mutex>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#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;
        GetSystemInfo(&sysInfo);
        return sysInfo.dwNumberOfProcessors;
    }
    
    static std::string get_cpu_model() {
        std::string model = "Unknown";
        HKEY hKey;
        char buffer[256];
        DWORD bufferSize = sizeof(buffer);
        
        if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, 
            "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 
            0, KEY_READ, &hKey) == ERROR_SUCCESS) {
            if (RegQueryValueExA(hKey, "ProcessorNameString", nullptr, 
                nullptr, (LPBYTE)buffer, &bufferSize) == ERROR_SUCCESS) {
                model = buffer;
                while (!model.empty() && model.back() == ' ') model.pop_back();
            }
            RegCloseKey(hKey);
        }
        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(); }
    double elapsed_ms() const {
        return std::chrono::duration<double, std::milli>(m_end - m_start).count();
    }
private:
    std::chrono::high_resolution_clock::time_point m_start, m_end;
};

// ==================== 测试套件 ====================
class CPUBenchmark {
private:
    std::vector<double> m_results;
    std::mutex m_mutex;
    
public:
    void add_result(double time_ms) {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_results.push_back(time_ms);
    }
    
    void print_statistics(const std::string& name) const {
        if (m_results.empty()) return;
        
        auto sorted = m_results;
        std::sort(sorted.begin(), sorted.end());
        
        double sum = std::accumulate(sorted.begin(), sorted.end(), 0.0);
        double mean = sum / sorted.size();
        
        double sq_sum = 0.0;
        for (double v : sorted) sq_sum += (v - mean) * (v - mean);
        double stddev = std::sqrt(sq_sum / sorted.size());
        
        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;
    
    timer.start();
    for (int i = 0; i < iterations; ++i) {
        result += i * 3 + 7;
        result ^= (i >> 2);
        result = (result * 1103515245 + 12345) & 0x7fffffff;
    }
    timer.stop();
    return timer.elapsed_ms();
}

// 2. 浮点运算
double test_float_ops(int iterations) {
    Timer timer;
    volatile float result = 1.0f;
    
    timer.start();
    for (int i = 1; i < iterations; ++i) {
        float x = i * 0.001f;
        result = std::sin(x) * std::cos(x) + std::sqrt(x + 1.0f);
        result = std::exp(-x * 0.1f) * std::log(x + 1.0f);
    }
    timer.stop();
    return timer.elapsed_ms();
}

// 3. 双精度浮点
double test_double_ops(int iterations) {
    Timer timer;
    volatile double result = 1.0;
    
    timer.start();
    for (int i = 1; i < iterations; ++i) {
        double x = i * 0.0001;
        result = std::sin(x) * std::cos(x) + std::sqrt(x + 1.0);
        result = std::exp(-x * 0.01) * std::log(x + 1.0);
    }
    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;
    
    for (int i = 0; i < size; ++i) data[i] = i;
    
    Timer timer;
    timer.start();
    for (int iter = 0; iter < 3; ++iter) {
        for (int i = 0; i < size; ++i) {
            sum += data[i];
        }
    }
    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);
    std::vector<double> C(size * size, 0.0);
    
    for (int i = 0; i < size * size; ++i) {
        A[i] = (i % size) * 0.1;
        B[i] = (i / size) * 0.01;
    }
    
    Timer timer;
    timer.start();
    for (int i = 0; i < size; ++i) {
        for (int k = 0; k < size; ++k) {
            double aik = A[i * size + k];
            for (int j = 0; j < size; ++j) {
                C[i * size + j] += aik * B[k * size + j];
            }
        }
    }
    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;
    
    Timer timer;
    timer.start();
    for (int i = 2; i * i <= max_num; ++i) {
        if (is_prime[i]) {
            for (int j = i * i; j <= max_num; j += i) {
                is_prime[j] = false;
            }
        }
    }
    timer.stop();
    return timer.elapsed_ms();
}

// 7. 排序
double test_sorting() {
    const int size = 100000;
    std::vector<int> data(size);
    std::random_device rd;
    std::mt19937 gen(rd());
    
    Timer timer;
    timer.start();
    for (int iter = 0; iter < 3; ++iter) {
        for (int i = 0; i < size; ++i) {
            data[i] = gen() % 1000000;
        }
        std::sort(data.begin(), data.end());
    }
    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_COUNT 
                     << "  " << std::fixed << std::setprecision(2) << result << " ms";
            std::cout.flush();
            
            std::this_thread::sleep_for(std::chrono::milliseconds(5));
        }
        std::cout << "\n";
        
        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;
}
原文