【问题标题】:How to know the execution time of a program [duplicate]如何知道程序的执行时间[重复]
【发布时间】:2020-09-15 12:39:25
【问题描述】:

如何以秒为单位打印 C 或 Python 代码的执行时间?

【问题讨论】:

标签: python c runtime code-snippets


【解决方案1】:

在 C 中,只需用这段代码包装您的代码。您将以秒为单位获得执行时间。

#include <time.h>
{
 clock_t start, end;
 double cpu_time_used;
 start = clock();

 /* Your Code */

 end = clock();
 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
 printf("The Execution Time In Seconds is : %lf",cpu_time_used);
}

【讨论】:

    【解决方案2】:

    您可以在 Python 中使用 datetime。

    start_time = datetime.datetime.now()
    <your program/ lines of code in the Python script/program>
    -----
    -----
    -----
    print (datetime.datetime.now() - start_time)
    

    这应该给时间。我猜你也可以使用 timeit.timeit()。

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 2018-05-24
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      相关资源
      最近更新 更多