【问题标题】:calculating execution time in c++在 C++ 中计算执行时间
【发布时间】:2010-10-26 23:43:23
【问题描述】:

我写了一个c++程序,我想知道如何计算执行所需的时间,这样我就不会超过时间限制。

#include<iostream>

using namespace std;

int main ()
{
    int st[10000],d[10000],p[10000],n,k,km,r,t,ym[10000];
    k=0;
    km=0;
    r=0;
    scanf("%d",&t);
    for(int y=0;y<t;y++)
    {
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
            cin>>st[i] >>d[i] >>p[i];
    }
    for(int i=0;i<n;i++)
    {
            for(int j=i+1;j<n;j++)
            {
                    if((d[i]+st[i])<=st[j])
                    {
                              k=p[i]+p[j];
                    }
                    if(k>km)
                    km=k;
            }
        if(km>r)
        r=km;
    }
    ym[y]=r;
}
    for( int i=0;i<t;i++)
    {
         cout<<ym[i]<<endl;
    }


    //system("pause");
    return 0;
}     

这是我的程序,我希望它在 3 秒内完成!!怎么做 ? 是的,对不起,我的意思是执行时间!!

【问题讨论】:

  • 什么时间限制?只有你可以施加时间限制,我想不出这样做的充分理由。
  • 它高度依赖于编译器/服务器/磁盘/其他负载。你通常不需要这样的限制。
  • 您确定需要测量编译时间吗?也许你的意思是执行时间?
  • @mekasperasky:您应该始终修复问题中所有错误的部分。不要简单地在最后添加一点编辑。修正标题,修正所有其他“编译”的地方。

标签: c++ execution-time


【解决方案1】:

如果你安装了 cygwin,从它的 bash shell,运行你的可执行文件,比如MyProgram,使用time 实用程序,像这样:

/usr/bin/time ./MyProgram

这将报告您的程序执行所用的时间——输出如下所示:

real    0m0.792s
user    0m0.046s
sys     0m0.218s

您也可以手动修改您的 C 程序以使用 clock() 库函数对其进行检测,如下所示:

#include <time.h>
int main(void) {
    clock_t tStart = clock();
    /* Do your stuff here */
    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

【讨论】:

  • 请注意,clock() 获取整体执行时间。因此,如果您将它与多线程代码一起使用,您可能会得到比预期更大的结果。
  • 感谢 FreeNickname,我想知道为什么当我扩大线程时我的程序运行得更慢了!
  • @Ashutosh Mehra,你的时钟代码总是输出为零。怎么办?
【解决方案2】:

使用 C++11 来测量一段代码的执行时间,我们可以使用 now() 函数:

auto start = chrono::steady_clock::now();

//  Insert the code that will be timed

auto end = chrono::steady_clock::now();

// Store the time difference between start and end
auto diff = end - start;

如果你想在上面的代码中打印开始和结束之间的时间差,你可以使用:

cout << chrono::duration <double, milli> (diff).count() << " ms" << endl;

如果您更喜欢使用纳秒,您将使用:

cout << chrono::duration <double, nano> (diff).count() << " ns" << endl;

diff变量的值也可以截断为整数值,例如,如果你希望结果表示为:

diff_sec = chrono::duration_cast<chrono::nanoseconds>(diff);
cout << diff_sec.count() << endl;

更多信息请点击here

【讨论】:

    【解决方案3】:

    概览

    我为此使用@AshutoshMehraresponse 编写了一个简单的语义破解。这样你的代码看起来真的很可读!

    #include <time.h>
    
    #ifndef SYSOUT_F
    #define SYSOUT_F(f, ...)      _RPT1( 0, f, __VA_ARGS__ ) // For Visual studio
    #endif
    
    #ifndef speedtest__             
    #define speedtest__(data)   for (long blockTime = NULL; (blockTime == NULL ? (blockTime = clock()) != NULL : false); SYSOUT_F(data "%.9fs", (double) (clock() - blockTime) / CLOCKS_PER_SEC))
    #endif
    

    用法

    speedtest__("Block Speed: ")
    {
        // The code goes here
    }
    

    输出

    Block Speed: 0.127000000s
    

    【讨论】:

      【解决方案4】:

      注意:这个问题最初是关于编译时间的,但后来发现 OP 真的意味着执行时间。但也许这个答案对某人仍然有用。

      对于 Visual Studio:转到 Tools / Options / Projects and Solutions / VC++ Project Settings 并将 Build Timing 选项设置为“yes”。之后,每个构建的时间将显示在“输出”窗口中。

      【讨论】:

        【解决方案5】:

        这看起来像 Dijstra 的算法。在任何情况下,运行所需的时间将取决于 N。如果需要超过 3 秒,我无法看到任何加速它的方法,因为它正在执行的所有计算都需要完成。

        根据您要解决的问题,可能会有更快的算法。

        【讨论】:

          【解决方案6】:

          我用过上面说的技术,还是发现Code:Blocks IDE中给出的时间和得到的结果差不多——(可能会相差一点点秒)..

          【讨论】:

            【解决方案7】:

            如果您使用的是 C++,那么您应该尝试下面的代码,因为如果您直接使用 @Ashutosh Mehra's answer,您总是会得到 0 作为答案。

            #include <iostream>
            #include <time.h>
            
            using namespace std;
            
            int main() {
                int a = 20000, sum=0;
                
                clock_t start = clock();
                for (int i=0; i<a; i++) {
                    for (int k = 0; k<a; k++)
                        sum += 1;
                }
                cout.precision(10);
                cout << fixed <<  float(clock() - start)/CLOCKS_PER_SEC  << endl;
                return 0;
            }
            

            因为在 C++ 中,浮点和双精度值将直接四舍五入。所以我使用cout.precision(10)将任意值的输出精度设置为小数点后10位。

            【讨论】:

            • (请注意,必须防止半体面的编译器根本不为嵌套循环生成任何代码——the problem sree mentions 的一种可能解释。这可能会在@987654326 之前受到&lt;&lt; " for " &lt;&lt; sum 的阻碍@. 不确定静态评估...)
            【解决方案8】:

            你可以试试下面的c++代码:

            #include <chrono>
            
            
            auto start = std::chrono::system_clock::now();
            // Your Code to Execute //
            auto end = std::chrono::system_clock::now();
            std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-07-22
              相关资源
              最近更新 更多