Timing the Thrash

Timing the Thrash

本文关键字:Thrash the Timing      更新时间:2023-10-16

我相信我的嵌套for条件会破坏内存,但我想知道它需要很长时间。我假设time.h会有所帮助,但我不知道该使用什么方法以及如何显示。有人能帮忙吗?

我已经根据建议更新了我的代码,我相信它是有效的。我的输出很慢,为4(thrashTime)。这是在几秒钟内吗?此外,也许我的方法可以重构。我设定了前后的时间条件。

// Updated
#include <iostream>
#include <time.h>
using namespace std;
int array[1 << 14][1 << 14];
int main() {
    time_t beforeThrash = 0;
    time_t afterThrash = 0;
    time_t thrashTime;
    int i, j;
    beforeThrash = time(NULL);
    for (i = 0; i<16384; i++)
        for (j = 0; j<16384; j++)
            array[i][j] = i*j;
    afterThrash = time(NULL);
    thrashTime = afterThrash - beforeThrash;
    cout << thrashTime << endl;
    system("pause");
    return 0;
}

您可以按照Joe Z提到的时间和时钟的指示进行操作。

打印当前时间的快速演示:

#include <ctime>
time_t start = time(0);
const char* tstart = ctime(&start);
// std::cout << tstart; will give you local time Fri Dec 06 11:53:46 2013

时差:

#include <ctime>
clock_t t = clock();
do_something();
t = clock() - t;
// std::cout << (float)t / CLOCKS_PER_SEC; will give you elapsed time in seconds

您可以简单地将do_something();替换为要测量的操作。

相关文章: