用c ++计算执行时间

Hic*_*ick 30 c++ execution-time

我写了一个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;
}     
Run Code Online (Sandbox Code Playgroud)

这是我的节目,我希望它在3秒的时限内!怎么做 ?是的,我的意思是执行时间!!

Ash*_*hra 103

如果您安装了cygwin,请从它的bash shell运行您的可执行文件,比如MyProgram使用该time实用程序,如下所示:

/usr/bin/time ./MyProgram
Run Code Online (Sandbox Code Playgroud)

这将报告程序执行的时间 - 输出结果如下所示:

real    0m0.792s
user    0m0.046s
sys     0m0.218s
Run Code Online (Sandbox Code Playgroud)

您也可以手动修改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;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,clock()获得总体执行时间.因此,如果您将它与多线程代码一起使用,您可能会获得比预期更大的结果. (9认同)

Saj*_*jal 20

使用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;
Run Code Online (Sandbox Code Playgroud)

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

cout << chrono::duration <double, milli> (diff).count() << " ms" << endl;
Run Code Online (Sandbox Code Playgroud)

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

cout << chrono::duration <double, nano> (diff).count() << " ns" << endl;
Run Code Online (Sandbox Code Playgroud)

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

diff_sec = chrono::duration_cast<chrono::nanoseconds>(diff);
cout << diff_sec.count() << endl;
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请点击


Mat*_*ian 15

概述

我使用@AshutoshMehra响应为此编写了一个简单的语义黑客.你的代码看起来很可读!

MACRO

#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
Run Code Online (Sandbox Code Playgroud)

用法

speedtest__("Block Speed: ")
{
    // The code goes here
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

Block Speed: 0.127000000s
Run Code Online (Sandbox Code Playgroud)


Ale*_*ter 9

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

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


Kan*_*nRG 6

您可以尝试以下 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;
Run Code Online (Sandbox Code Playgroud)