How can I measure the runtime of a program in c ++?

0

I made a program that randomly generates a vector and then orders it, using bubble sort, now I have to measure the time spent with the ordering.

    
asked by anonymous 23.02.2018 / 02:30

2 answers

2

Are you using linux as an OS? For a simple program like this I suggest you use time ./a.out in the terminal, where a.out is the executable of your program. This command prints 3 lines (explained briefly):

  • real : Actual start time at end of execution;
  • sys : Time spent by the system kernel in the process of executing your program;
  • user : Time used in user-mode . This is the amount of time your computer spends in the process outside the kernel;
23.02.2018 / 09:31
2

You can calculate the runtime of functions using C ++ 11 with high precision using the library. <chrono> . For example:

auto inicio = std::chrono::high_resolution_clock::now();
...SEU CÓDIGO AQUI...
auto resultado = std::chrono::high_resolution_clock::now() - inicio;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(resultado).count();

Since you can replace std::chrono::microseconds (microseconds) with std::chrono::milliseconds (milliseconds), std::chrono::seconds (seconds) or even std::chrono::nanoseconds (nanoseconds).

    
13.03.2018 / 02:55