How To find Execution Time In C/C++
- Get link
- X
- Other Apps
There is two way to find execution time in C/C++.
We give differed inputs to get differed executions time.
1. 512*512
2.1024*1024
3.2048*2048
1st way Copy The code and Run It give input in array:
#include <stdio.h>
#include <time.h>
int m={1},n={1};
int array[2048][2048];
// A function that terminates when enter key is pressed
void fun()
{
printf("fun() starts \n");
printf("Press enter to stop fun \n");
for(int kk=0;kk<1000;kk++)
{
int sum={0};
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
sum+=array[2048][2048];
}
}
printf("fun() ends \n");
}
// The main program calls fun() and measures time taken by fun()
int main()
{
// Calculate the time taken by fun()
clock_t t;
t = clock();
fun();
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("fun() took %f seconds to execute \n", time_taken);
return 0;
}
2nd way Copy The code and Run It give input in array
#include <iostream>
#include <time.h>
using namespace std;
// A function that terminates when enter key is pressed
void fun()
{
cout<<"fun() starts \n";
int A[512][512],N,M,sum,i,j,kk;
for(kk=0;kk<1000;kk++)
{ sum = 0;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
sum += A[j][i];
}
cout<<"fun() ends \n";
}
int main()
{
// Calculate the time taken by fun()
clock_t t;
t = clock();
fun();
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
cout<<" took %f seconds to execute "<<time_taken;
}
- Get link
- X
- Other Apps
Comments