optimization - Measuring execution time of a function in C++ -


i want find out how time function takes in c++ program execute on linux. afterwards, want make speed comparison . saw several time function ended boost. chrono:

process_user_cpu_clock, captures user-cpu time spent current process 

now, not clear if use above function, time cpu spent on function?

secondly, not find example of using above function. can 1 please me how use above function?

p.s: right , using std::chrono::system_clock::now() time in seconds gives me different results due different cpu load every time.

it easy use method in c++11. have use std::chrono::high_resolution_clock <chrono> header.

use so:

#include <iostream> #include <chrono>  using namespace std; using namespace std::chrono;  void function() {     long long number = 0;      for( long long = 0; != 2000000; ++i )     {        number += 5;     } }  int main() {     high_resolution_clock::time_point t1 = high_resolution_clock::now();     function();     high_resolution_clock::time_point t2 = high_resolution_clock::now();      auto duration = duration_cast<microseconds>( t2 - t1 ).count();      cout << duration;     return 0; } 

this measure duration of function.

note: not requirement same output because cpu of machine can less or more used other processes running on computer. solve math exercise, mind can more or less concentrated solve in different times. in human mind, can remember solution of math problem, though computer same process new, so, said, not required same result always!


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -