【问题标题】:Printing time in seconds以秒为单位的打印时间
【发布时间】:2013-03-22 21:12:46
【问题描述】:

我正在编写一个程序并尝试计算给定代码块运行时经过的秒数。之后,我想以秒为单位打印运行代码块所花费的总时间。我写的是:

time_t start = time(0);
// block of code
double seconds_since_start = difftime(time(0), start);
printf("seconds since start: %2.60f\n", seconds_since_start);

我已将printf() 打印到小数点精度 60,但所有时间仍为 0.000000...

我的时间函数有错误吗?我很难相信我要求计时的任务不会以 60 位小数精度计算任何时间。

【问题讨论】:

  • time() 返回秒数。如果经过的时间小于 1 秒,您将始终在我们的输出中看到 0。
  • 您可以随时使用<chrono>
  • @craig65535:并非总是如此!如果您在第二次更改之前开始计时,则不会。
  • @TonyK:哦,太好了,所以即使实际经过的时间小 1000 倍,现在您也会看到“1 秒”。
  • @TonyK 是的。我应该说的是,你会看到时钟变化,而不是经过的时间。因此,对于耗时 900 毫秒的作业,您可能会看到“自启动以来 0 秒”,对于耗时 1 毫秒的作业,您可能会看到“自启动以来 1 秒”,具体取决于您的时钟与秒变化的接近程度。

标签: c++ time time-t


【解决方案1】:

您可以使用 C++11 中提供的日期和时间实用程序:

#include <chrono>
#include <iostream>
#include <thread>

int main()
{
    auto start = std::chrono::high_resolution_clock::now();

    std::this_thread::sleep_for(std::chrono::seconds(5));

    auto end = std::chrono::high_resolution_clock::now();

    auto difference = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();

    std::cout << "Seconds since start: " << difference;
}

【讨论】:

  • 您可能需要单调计时器,而不是高分辨率计时器。
  • @BenVoigt:对不起,高分辨率计时器和单调计时器有什么区别? Castiblanco 的解决方案有效,但我不喜欢它依赖于编写您自己的函数的事实。使用包含实现所需功能的标准包不是更好吗?
  • @raphnguyen:单调计时器测量经过的时间。其他时间以系统日历为准。通常相同,但如果用户(或 NTP 客户端服务)在您的程序运行时更新系统日期或时间...
  • @David:感谢演示。我已将其修改为返回微秒,然后将其除以 1000000 以获得小数秒:liveworkspace.org/code/YT1I$6。它似乎在演示中运行良好,但在我的程序中我只得到00.001 的值。这是有原因的吗?
  • @raphnguyen 如果您希望它返回微秒,那么只需将duration_cast&lt;&gt; 中的std::chrono::seconds 替换为std::chrono::microseconds
【解决方案2】:

time 的返回值是整数秒。转换为 double 不会恢复丢失的小数秒。

您需要更精确的时钟功能,例如 gettimeofday(如果您想要挂钟时间)或 times(如果您想要 CPU 时间)。

在 Windows 上,有 timeGetTimeQueryPerformanceCounter(Castiblanco 演示)或 GetSystemTimeAsFileTime

C++ 终于有了一些标准的高分辨率时钟函数,带有 C++11 的 &lt;chrono&gt; 标头,由 chris 在 cmets 中提出。

【讨论】:

  • 在 Windows 上是否有 gettimeofday 的包含包,或者这是一个 UNIX 包?
  • @raphnguyen:添加了一些在 Windows 上可用的选项。
【解决方案3】:

其实我更喜欢用毫秒来做,因为有很多函数如果你只用秒就可以返回 0,因此最好用毫秒。

#include <time.h>

double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b){
  LARGE_INTEGER freq;
  QueryPerformanceFrequency(&freq);
  return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}


int main()
{

LARGE_INTEGER t_inicio, t_final;
double sec;

QueryPerformanceCounter(&t_inicio);    

// code here, the code that you need to knos the time.

QueryPerformanceCounter(&t_final);

sec = performancecounter_diff(&t_final, &t_inicio);

printf("%.16g millisegudos\n", sec * 1000.0);*/

}

return 0;
}

【讨论】:

  • 不错的方法(如果这是 Windows),但您忘记致电 performancecounter_diff
【解决方案4】:

你可以使用boost::timer

template<typename T>
double sortTime(std::vector<T>& v, typename sort_struct<T>::func_sort f){
    boost::timer t; // start timing
    f(v);
    return t.elapsed();
}

【讨论】:

    【解决方案5】:

    类似的东西应该可以工作:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() 
    { 
        clock_t begin, end;
        double time_spent;
    
        begin = clock();
    
        //Do stuff
    
        end = clock();
        time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
        printf("%Lf\n",time_spent);
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多