【问题标题】:Time not being calculated时间未计算
【发布时间】:2015-01-18 19:17:12
【问题描述】:

我想看看哪个访问更快,一个结构体还是一个元组,所以我写了一个小程序。但是,当它完成运行时,两者的记录时间都是 0.000000。我很确定程序没有那么快完成(因为我不在家,所以在在线编译器上运行)

#include <iostream>
#include <time.h>
#include <tuple>
#include <cstdlib>
using namespace std;

struct Item
{
    int x;
    int y;
};

typedef tuple<int, int> dTuple;

int main() {

    printf("Let's see which is faster...\n");
    //Timers
    time_t startTime;
    time_t currentTimeTuple;
    time_t currentTimeItem;

    //Delta times
    double deltaTimeTuple;
    double deltaTimeItem;

    //Collections
    dTuple tupleArray[100000];
    struct Item itemArray[100000];

    //Seed random number
    srand(time(NULL));
    printf("Generating tuple array...\n");
    //Initialize an array of tuples with random ints
    for(int i = 0; i < 100000; ++i)
    {
        tupleArray[i] = dTuple(rand() % 1000,rand() % 1000);
    }

    printf("Generating Item array...\n");
    //Initialize an array of Items
    for(int i = 0; i < 100000; ++i)
    {
        itemArray[i].x = rand() % 1000;
        itemArray[i].y = rand() % 1000;
    }

    //Begin timer for tuple array
    time(&startTime);
    //Iterate through the array of tuples and print out each value, timing how long it takes
    for(int i = 0; i < 100000; ++i)
    {
        printf("%d: %d", get<0>(tupleArray[i]), get<1>(tupleArray[i]));
    }
    //Get the time it took to go through the tuple array
    time(&currentTimeTuple);
    deltaTimeTuple = difftime(startTime, currentTimeTuple);

    //Start the timer for the array of Items
    time(&startTime);
    //Iterate through the array of Items and print out each value, timing how long it takes
    for(int i = 0; i < 100000; ++i)
    {
        printf("%d: %d", itemArray[i].x, itemArray[i].y);
    }
    //Get the time it took to go through the item array
    time(&currentTimeItem);
    deltaTimeItem = difftime(startTime, currentTimeItem);
    printf("\n\n");
    printf("It took %f seconds to go through the tuple array\nIt took %f seconds to go through the struct Item array\n", deltaTimeTuple, deltaTimeItem);
    return 0;
}

根据 www.cplusplus.com/reference/ctime/time/,difftime 应该返回两个 time_t 之间的差。

【问题讨论】:

  • 永远不要在基准循环中包含printf 语句。他们只会破坏任何时间。
  • 我认为没有理由怀疑您的程序可以在不到一秒的时间内完成。
  • 我不明白,tuple 是一个结构。如果时机很重要,我会很感兴趣。检查汇编语言列表以验证访问 tuple 与访问您的结构的汇编语言是否不同。
  • 您的程序可以运行,但时钟的分辨率可能太低。如果我添加一个睡眠,时间是准确的。但是请注意,您应该交换 difftime 的参数,除非您希望报告负间隔。由于您似乎无论如何都在使用 C++11,请考虑切换到 std::chrono 以获得更准确(和方便)的时间。

标签: c++ time benchmarking


【解决方案1】:

time() 通常返回自 UTC 时间 1970 年 1 月 1 日 00:00 开始的秒数(即当前的 unix 时间戳)。因此,根据您的库实现,任何低于 1 秒的内容都可能显示为 0。

您应该更喜欢使用&lt;chrono&gt; 进行基准测试:

chrono::high_resolution_clock::time_point t;
t = high_resolution_clock::now();
// do something to benchmark 
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
cout <<"Exec in ms: "<< chrono::duration_cast<milliseconds>(t2 - t).count() <<endl;

您仍然必须考虑时钟分辨率。例如,对于 windows,它是 15 毫秒,所以如果你接近 15 毫秒,甚至更低,你应该增加迭代次数。

【讨论】:

    【解决方案2】:

    我建议在开始性能计时之前检查汇编语言列表。

    首先要检查的是,访问tuple 与访问您的结构之间的汇编语言有很大不同。您可以通过计算不同指令的数量来粗略估计时序差异。

    其次,我建议查看Tuple 的定义。有些东西告诉我,它是一个声明类似于你的结构。因此,我预测您的时间应该几乎相等。

    第三,您还应该比较std::pair,因为您的tuple 中只有2 个项目,而std::pair 有两个元素。同样,它应该与您的结构相同。

    最后,为数据中的“噪音”做好准备。噪音可能来自数据缓存未命中、使用数据缓存的其他程序、内核之间的代码委托以及任何 I/O。您越接近在单核上运行程序,不受中断,您的数据质量就越好。

    【讨论】:

      【解决方案3】:

      time_t 是秒数,一台快速的现代计算机上的 100,000 次琐碎操作确实可以在不到一秒的时间内完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 2014-02-18
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 2011-05-22
        相关资源
        最近更新 更多