Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculating execution time in c++

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<iostream>  using namespace std;  int main () {     int st[10000],d[10000],p[10000],n,k,km,r,t,ym[10000];     k=0;     km=0;     r=0;     scanf("%d",&t);     for(int y=0;y<t;y++)     {     scanf("%d",&n);     for(int i=0;i<n;i++)     {             cin>>st[i] >>d[i] >>p[i];     }     for(int i=0;i<n;i++)     {             for(int j=i+1;j<n;j++)             {                     if((d[i]+st[i])<=st[j])                     {                               k=p[i]+p[j];                     }                     if(k>km)                     km=k;             }         if(km>r)         r=km;     }     ym[y]=r; }     for( int i=0;i<t;i++)     {          cout<<ym[i]<<endl;     }       //system("pause");     return 0; }      

this is my program and i want it to be within time limit 3 sec !! how to do it ? yeah sorry i meant execution time !!

like image 543
Hick Avatar asked May 18 '09 09:05

Hick


People also ask

How is execution time calculated?

1) Create a loop around whatneeds to be measured, that executes 10, 100, or 1000 times or more. Measure execution time to the nearest 10 msec. Then divide that time bythe number of times the loop executed. If the loop executed 1000 timesusing a 10 msec clock, you obtain a resolution of 10 µsec for theloop.

How do you calculate elapsed time in C?

To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.

How do you calculate execution time of any C C++ program?

measure execution time of a program. Using time() function in C & C++. time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Prototype / Syntax : time_t time(time_t *tloc);

What is execution time in programming?

Execution time refers to the stage at which the instructions in the computer programs/code are executed. At execution time, run-time libraries are used. Some basic operations that occur at execution time include reading program instructions to carry out tasks or complete actions.


2 Answers

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; } 
like image 169
Ashutosh Mehra Avatar answered Oct 02 '22 15:10

Ashutosh Mehra


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

like image 21
Sajal Avatar answered Oct 02 '22 15:10

Sajal