calculating execution time in c++

后端 未结 6 550
庸人自扰
庸人自扰 2020-12-22 19:26

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

usin         


        
相关标签:
6条回答
  • 2020-12-22 19:58

    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
    
    0 讨论(0)
  • 2020-12-22 20:00

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

    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;
    

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

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

    If you prefer to use nanoseconds, you will use:

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

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

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

    For more info click here

    0 讨论(0)
  • 2020-12-22 20:06

    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;
    }
    
    0 讨论(0)
  • 2020-12-22 20:08

    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)..

    0 讨论(0)
  • 2020-12-22 20:10

    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.

    0 讨论(0)
  • 2020-12-22 20:10

    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.

    0 讨论(0)
提交回复
热议问题