繁体   English   中英

计算 c++ 中的执行时间

[英]calculating execution time in c++

我写了一个 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 秒内完成?! 怎么做 ? 是的,对不起,我的意思是执行时间!

如果你安装了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;
}

使用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;

欲了解更多信息,请点击

概述

我使用@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

用法

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

OUTPUT

Block Speed: 0.127000000s

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

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

您可以为 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;

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

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

我已经使用了上面提到的技术,我仍然发现Code:Blocks IDE中给出的时间或多或少类似于获得的结果 - (可能会有微小的差异)。

如果您使用的是 C++ 那么您应该尝试下面的代码,因为如果您直接使用@Ashutosh Mehra 的答案,您总是会得到 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)将任意值的 output 精度设置为小数点后 10 位。

Ashutosh Mehra 回答的简短版本:

/* including stuff here */
#include <time.h>
int main(void) {
    clock_t tStart = clock();
    /* stuff here */
    cout<<"Time taken: "<<(double)(clock() - tStart)/CLOCKS_PER_SEC;
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM