计算 c++ 中的执行时间

发布于 2024-07-20 23:08:29 字数 932 浏览 3 评论 0原文

我写了一个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 秒的时间限制内! 怎么做 ? 是的,抱歉,我的意思是执行时间!

I have written a c++ program , I want to know how to calculate the time taken for execution so I won't exceed the time limit.

#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;
}     

this is my program and i want it to be within time limit 3 sec !! how to do it ?
yeah sorry i meant execution time !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

复古式 2024-07-27 23:08:29

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

/usr/bin/time ./MyProgram

这将报告您的程序执行了多长时间take - 输出将如下所示:

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;
}

If you have cygwin installed, from it's bash shell, run your executable, say MyProgram, using the time utility, like so:

/usr/bin/time ./MyProgram

This will report how long the execution of your program took -- the output would look something like the following:

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

You could also manually modify your C program to instrument it using the clock() library function, like so:

#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;
}
痕至 2024-07-27 23:08:29

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

auto start = std::chrono::steady_clock::now();
 
//  Insert the code that will be timed

auto end = std::chrono::steady_clock::now();
  
// Store the time difference between start and end
auto diff = end - start;

如果你想打印上面代码中 start 和 end 之间的时间差,你可以使用:

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

如果你愿意使用纳秒,您将使用:

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

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

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

有关更多信息,请单击 此处

With C++11 for measuring the execution time of a piece of code, we can use the now() function:

auto start = std::chrono::steady_clock::now();
 
//  Insert the code that will be timed

auto end = std::chrono::steady_clock::now();
  
// Store the time difference between start and end
auto diff = end - start;

If you want to print the time difference between start and end in the above code, you could use:

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

If you prefer to use nanoseconds, you will use:

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

The value of the diff variable can be also truncated to an integer value, for example, if you want the result expressed as:

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

For more info click here

爱你是孤单的心事 2024-07-27 23:08:29

概述

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

#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

OVERVIEW

I have written a simple semantic hack for this using @AshutoshMehraresponse. You code looks really readable this way!

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

USAGE

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

OUTPUT

Block Speed: 0.127000000s
倚栏听风 2024-07-27 23:08:29

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

对于 Visual Studio:转到工具/选项/项目和解决方案/VC++ 项目设置并将构建计时选项设置为“yes”。 之后,每次构建的时间将显示在“输出”窗口中。

Note: the question was originally about compilation time, but later it turned out that the OP really meant execution time. But maybe this answer will still be useful for someone.

For Visual Studio: go to Tools / Options / Projects and Solutions / VC++ Project Settings and set Build Timing option to 'yes'. After that the time of every build will be displayed in the Output window.

通知家属抬走 2024-07-27 23:08:29

您可以尝试以下 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;

You can try below code for 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;
风为裳 2024-07-27 23:08:29

这看起来像 Dijstra 算法。 无论如何,运行所花费的时间将取决于 N。如果花费超过 3 秒,我就没有任何方法可以加快它的速度,因为它正在执行的所有计算都需要完成。

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

This looks like Dijstra's algorithm. In any case, the time taken to run will depend on N. If it takes more than 3 seconds there isn't any way I can see of speeding it up, as all the calculations that it is doing need to be done.

Depending on what problem you're trying to solve, there might be a faster algorithm.

隐诗 2024-07-27 23:08:29

我已经使用了上面所说的技术,但我仍然发现Code:Blocks IDE中给出的时间与获得的结果或多或少相似 - (可能会相差一点微秒)。 。

I have used the technique said above, still I found that the time given in the Code:Blocks IDE was more or less similar to the result obtained-(may be it will differ by little micro seconds)..

☆獨立☆ 2024-07-27 23:08:29

如果您使用的是 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++ 中,float 和 double 值将直接四舍五入。 因此,我使用 cout. precision(10) 将任何值的输出精度设置为小数点后 10 位。

If you are using C++ then you should try this below code as you would always get 0 as answer if you directly use @Ashutosh Mehra's answer.

#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;
}

Because in C++ you the float and double values will directly be rounded off. So I used the cout.precision(10) to set the output precision of any value to 10 digits after decimal.

一页 2024-07-27 23:08:29

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;
}

shorter version of Ashutosh Mehra's answer:

/* 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文