答:用户和内核cpu时间是分开测量的,小心!
编辑:pTimes 现在在每个基准测试之间重置为零,但结果变得更奇怪了!
以摆弄我自己的自定义内存管理方案为最终目标,我为 Visual Community 2019、Windows 10 中现有的 malloc() 做了一个简单的基准测试。出于兴趣,我对 CPU 时间和 wall 时间进行了基准测试,然后我通过在许多块中分配一大块内存,然后单独释放每个块而不使用它们来测试 malloc。看这里:
void malloc_distr256(int nMemsize) {
long long pFreeList[256];
for (int i = 0; i < 256; ++i) pFreeList[i] = malloc(nMemsize >> 8);
for (int i = 0; i < 256; ++i) free((void*)pFreeList[i]);
}
void malloc_distr64(int nMemsize) {
long long pFreeList[64];
for (int i = 0; i < 64; ++i) pFreeList[i] = malloc(nMemsize >> 6);
for (int i = 0; i < 64; ++i) free((void*)pFreeList[i]);
}
void malloc_distr0(int nMemsize) {
void* pMem = malloc(nMemsize);
free(pMem);
}
我使用以下代码对这些函数进行了基准测试——“BenchTimes”只是一个拥有双倍 CPU/wall 时间的结构:
inline double cputime() {
FILETIME lpCreationTime;
FILETIME lpExitTime;
FILETIME lpKernelTime;
FILETIME lpUserTime;
if (GetProcessTimes(GetCurrentProcess(), &lpCreationTime, &lpExitTime, &lpKernelTime, &lpUserTime)) {
double dUnits = (double)(lpUserTime.dwLowDateTime | (long long)lpUserTime.dwHighDateTime << 32);
return dUnits * 0.1;
}
else return 0xFFF0000000000000;
}
inline double walltime() {
LARGE_INTEGER lnFreq, lnTime;
if (QueryPerformanceFrequency(&lnFreq)) if (QueryPerformanceCounter(&lnTime))
return 1000000.0 * (double)lnTime.QuadPart / (double)lnFreq.QuadPart;
//multiply by 1,000,000 to convert seconds to microseconds
//because the cpu time measurer I had in microseconds as well
return 0.0;
}
void bench(void (pfnFunc)(int), int nMemsize, int nIters, int nReps, BenchTimes* pTimes) {
pTimes->dCpuTime = 0.0;
pTimes->dWallTime = 0.0;
for (volatile int r = 0; r < nReps; ++r) {
double dCpuStart = cputime();
double dWallStart = walltime();
for (volatile int i = 0; i < nIters; ++i) pfnFunc(nMemsize);
double dCpuEnd = cputime();
double dWallEnd = walltime();
double dCpuDiff = dCpuEnd - dCpuStart;
double dWallDiff = dWallEnd - dWallStart;
pTimes->dCpuTime += dCpuDiff;
pTimes->dWallTime += dWallDiff;
}
}
这些是在我的电脑(i5-9400f)上测量的时间,以秒为单位。
我对性能和 Wall time 与 CPU 时间比较的巨大差异感到非常好奇!
运行它的代码在这里:
BenchTimes sTimes;
bench(malloc_distr256, 1 << 20, 100, 1000, &sTimes);
fprintf(stdout, "Malloc alloc/free bench allocated %lf megabytes, distributed over 256 chunks\n", (double)(1 << 20) / 1000000);
fprintf(stdout, "Malloc alloc/free bench returned:\nWalltime - total: %lf\nCPU Time - total: %lf\n", sTimes.dWallTime / 1000000, sTimes.dCpuTime / 1000000);
bench(malloc_distr64, 1 << 20, 100, 1000, &sTimes);
fprintf(stdout, "\nMalloc alloc/free bench allocated %lf megabytes, distributed over 64 chunks\n", (double)(1 << 20) / 1000000);
fprintf(stdout, "\nMalloc alloc/free bench returned:\nWalltime - total: %lf\nCPU Time - total: %lf\n", sTimes.dWallTime / 1000000, sTimes.dCpuTime / 1000000);
bench(malloc_distr0, 1 << 20, 100, 1000, &sTimes);
fprintf(stdout, "\nMalloc alloc/free bench allocated %lf megabytes, distributed over no chunks\n", (double)(1 << 20) / 1000000);
fprintf(stdout, "\nMalloc alloc/free bench returned:\nWalltime - total: %lf\nCPU Time - total: %lf\n", sTimes.dWallTime / 1000000, sTimes.dCpuTime / 1000000);
system("pause");