calculating execution time in c++

后端 未结 6 554
庸人自扰
庸人自扰 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 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 
    int main(void) {
        clock_t tStart = clock();
        /* Do your stuff here */
        printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
        return 0;
    }
    

提交回复
热议问题