Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the execution time of my code?

I am using visual studio 2013 and i need to find out the execution time of my code(C++). Is there anyway that make me do that ?

like image 431
codex Avatar asked Oct 18 '25 12:10

codex


2 Answers

This maybe one of the possible answer :

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.

For C

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

For C++

For C++11

like image 191
udit043 Avatar answered Oct 21 '25 00:10

udit043


Try to use function clock() from <time.h>:

#include <time.h>
#include <iostream>
int main()
{
    clock_t clkStart;
    clock_t clkFinish;

    clkStart = clock();
    for(int i = 0; i < 10000000; i++)
        ;
    //other code
    clkFinish = clock();
    std::cout << clkFinish - clkStart;

    system("pause");
    return 0;
}
like image 28
Konstantin Dedov Avatar answered Oct 21 '25 00:10

Konstantin Dedov