电脑性能测试小工具

作用

购买一台新电脑或走进一间新机房后,这个小工具可以帮您快速了解电脑的性能。

注意:本工具仅供娱乐,不能作为电脑性能的参考依据。

原理

本工具会计算 \(2\times N\times M\) 次加法运算和 \(N\times M\) 次取模运算,并进行内存的顺序读写和随机读写。

用法

输出格式为:

Initialization completed, testing...
Counting: 5e+08                        # N*M 的值,即取模运算的次数,默认为 5e8  
Random number: 200136158               # 求出的 sum 的值,可以忽略这一项  
Running for 9.751 second(s).           # 程序在求和阶段的运行时长,保留三位小数  

代码

#include <cstdio>
#include <random>
#include <ctime>
const int N = 10000000, NN = N + 5, M = 50, mod = 998244353;
int a[NN], b[NN], sum = 0;
std::mt19937_64 rng(mod);
#define randInt(l, r) ( std::uniform_int_distribution<int>((l),(r))(rng) )
int main() {
    srand(time(NULL));
    for (int i = 1; i <= N; i++) a[i] = rand();
    for (int i = 1; i <= N; i++) b[i] = i;
    for (int i = N; i >= 1; i--) std::swap(b[i], b[randInt(1, i)]);
    printf("Initialization completed, testing...\n");
    clock_t beg = clock();
    for (int j = 1; j <= M; j++) for (int i = 1; i <= N; i++) sum = (sum + a[b[i]] + j ^ i) % mod;
    clock_t end = clock();
    double tim = static_cast<double>(end - beg) / CLOCKS_PER_SEC;
    printf("Counting: %g\nRandom number: %d\n", (float)((long long)N * M), sum);
    printf("Running for %.3lf second(s).\n", tim);
    return 0;
}

建议的编译选项:-std=c++14 -O2 -static

posted @ 2025-09-04 11:44  cwkapn  阅读(8)  评论(0)    收藏  举报