跳到正文

个人记录

DEEPSEEK的人机测试

Neko_QwQ约 18 分钟
原文

这是 2026年6月27日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  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;
}