Execution time in c++ -


trying find execution time of code using :

#include <iostream> #include <time.h> using namespace std;  int main() {     clock_t t1, t2;      t1 = clock();      // code goes here      t2 = clock();      float diff = ((float)t2 - (float)t1);      cout <<"execution time = "<<diff / clocks_per_sec <<endl;     system ("pause");      return 0;     }  

but returns different time every time executed same code. code correct?

i want check execution time of code in different scenarios shouldn't display same time when execute same code twice?

as mentioned here, clock ticks units of time of constant system-specific length, returned function clock. having mentioned need consider couple of scenarios/facts when using method find out time of execution of piece of code.

1) time tick represents depends on os. there os-internal counters clock ticks. please see superuser question.

2) resources need allocated process run on system. if processor busy more important process or might have run out of resources. in case process put in queue , run lower priority. clockticks stored in internal counter(as mentioned above), goes on getting incremented though other processes utilizing processor meanwhile.

conclusion

your method of finding execution time based on clock ticks not yield exact results give idea execution times.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -