【问题标题】:Running time of functions函数运行时间
【发布时间】:2023-04-02 18:08:01
【问题描述】:

我想打印我的函数的运行时间。出于某种原因,我的计时器总是返回 0。谁能告诉我为什么?

double RunningTime(clock_t time1, clock_t time2)
{
    double t=time1 - time2;
    double time = (t*1000)/CLOCKS_PER_SEC;
    return time;
}

int main()
{
     clock_t start_time = clock();


     // some code.....


    clock_t end_time = clock();

    std::cout << "Time elapsed: " << double(RunningTime(end_time, start_time)) << " ms";

    return 0;
}

我尝试使用gettimeofday,但它仍然返回 0。

double get_time()
{
    struct timeval t;
    gettimeofday(&t, NULL);
    double d = t.tv_sec + (double) t.tv_usec/100000;
    return d;
}

int main()
{
        double time_start = get_time();

        //Some code......

        double time_end = get_time();

        std::cout << time_end - time_start;

    return 0;
}

还尝试使用chrono,它给了我各种构建错误:

  • 错误:#error 此文件需要编译器和库支持 即将推出的 ISO C++ 标准,C++0x。目前此支持
    实验性的,必须使用 -std=c++0x 或 -std=gnu++0x 启用 编译器选项。
  • 警告:'auto' 将改变 C++0x 中的含义;请删除它
  • 错误:ISO C++ 禁止声明 't1' 而没有类型错误: 'std::chrono' 尚未声明
  • 错误:在 '(t2 - t1)' 中请求成员 'count',它是 非类类型'int'

    int main() { 自动 t1 = std::chrono::high_resolution_clock::now();

                //Some code......
    
                auto t2 = std::chrono::high_resolution_clock::now();
    
                std::cout << "Time elapsed: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n";
    
            return 0;
        }
    

【问题讨论】:

  • 如果您想要良好的分辨率,请考虑使用&lt;chrono&gt;。您可以轻松地为单位指定毫秒而不是计算它。
  • 在 *nix 系统上,尝试gettimeofday() 以获得高分辨率时间(微秒)。
  • 如果你没有 C++11,你可以考虑在 Linux 上使用clock_gettime(使用CLOCK_MONOTONIC_HR),或者在大多数其他 UNIX 变体上考虑gethrtime,在 Windows 上考虑QueryPerformanceCounter .
  • @chris 我将如何实现 &lt;chrono&gt; ?我很快就用谷歌搜索了它,但无法快速弄清楚如何将它放入我的程序中而不会导致构建错误。我用std::chrono::time_point&lt;Clock&gt; time_point
  • @Fourthmeal70,我明白了,您的编译器选项中需要-std=c++11-std=c++0x 标志。

标签: c++ time clock


【解决方案1】:

计时器滴答大约等于 1/CLOCKS_PER_SEC 秒,即毫秒分辨率。要查看一个实数(非零),您应该调用一个非常长时间的函数或使用另一个具有更高时间分辨率功能的库:

  • 新的 c++11x 库 chrono(使用 MSVS 2012)
  • boost::chrono(不幸的是,图书馆引用了很多其他的)
  • POSIX 函数 gettimeofday,提供 1 微秒的时间分辨率

【讨论】:

  • gettimeofday的弱点是它不是单调的,当某些东西(比如ntpd)调整系统时间时会产生奇怪的结果。
  • True,例如,如果机器试图与网络时间服务器保持同步。但在这种情况下,它可能是合适的。
  • 您可以在代码中插入 1 毫秒的延迟并再次测量吗?或者,例如,使用循环重复您想要测量的代码 100 次。我认为您的代码执行速度非常快,因此您无法计算完成需要多少时间。
  • timeval 结构的值是多少?我的意思是两个领域。
  • 我正在计时一个排序函数,并使用了一个包含 100,000 个数字的文件,计时器说它花了 0.312 毫秒。这似乎很短。我编辑了上面的代码以显示我尝试过的所有方法。
【解决方案2】:

经过多次反复试验,我选择了gettimeofday。这是我终于可以正常工作的代码。

double get_time()
{
    struct timeval t;
    gettimeofday(&t, NULL);
    double d = t.tv_sec + (double) t.tv_usec/1000000;
    return d;
}

int main()
{
    double time_start = get_time();

    //Some code.........

    double time_end = get_time();

    std::cout << time_end - time_start;

    return 0;
}

【讨论】:

    【解决方案3】:

    我最近使用的一个解决方案是使用 C++11 的 lambda 功能来为任意函数调用或一系列动作计时。

    #include <ctime>
    #include <iostream>
    #include <functional>
    
    void timeit(std::function<void()> func) {
        std::clock_t start = std::clock();
    
        func();
    
        int ms = (std::clock() - start) / (double) (CLOCKS_PER_SEC / 1000);
    
        std::cout << "Finished in " << ms << "ms" << std::endl;
    }
    
    int main() {
        timeit([] {
            for (int i = 0; i < 10; ++i) {
                std::cout << "i = " << i << std::endl;
            } 
        });
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-22
      相关资源
      最近更新 更多