How to print the execution time of my code?
18,813
Solution 1
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
Solution 2
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;
}
Author by
codex
Updated on June 26, 2022Comments
-
codex almost 2 years
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 ?
-
Kerrek SB over 8 yearsWhy cast to microseconds and then do manual arithmetic?! Why not just cast to seconds?
-
Galik over 8 years@KerrekSB If I do that it gets rounded to the nearest second so I lose the decimal places.
-
tanweer alam almost 7 yearsDo you need enable build time to get execution time time stamp?