美文网首页
Measure execution time of a func

Measure execution time of a func

作者: XBruce | 来源:发表于2021-01-19 18:10 被阅读0次

We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we should strive to use C++ specific language constructs only.

std::chrono has two distinct objects–timepoint and duration. A timepoint as the name suggests represents a point in time whereas a duration represents an interval or span of time. The C++ library allows us to subtract two timepoints to get the interval of time passed in between. Using provided methods we can also convert this duration to appropriate units.

The std::chrono provides us with three clocks with varying accuracy. The high_resolution_clock is the most accurate and hence it is used to measure execution time.

Step 1: Get the timepoint before the function is called

filter_none

brightness_4

|

#include <chrono>

using namespace std::chrono;

// Use auto keyword to avoid typing long

// type definitions to get the timepoint

// at this instant use function now()

auto start = high_resolution_clock::now();

|

Step 2: Get the timepoint after the function is called

filter_none

brightness_4


`#include <chrono>`

`using` `namespace` `std::chrono;`

`// After function call`

`auto` `stop = high_resolution_clock::now();`

Step 3: Get the difference in timepoints and cast it to required units

filter_none

brightness_4

|

// Subtract stop and start timepoints and

// cast it to required unit. Predefined units

// are nanoseconds, microseconds, milliseconds,

// seconds, minutes, hours. Use duration_cast()

// function.

auto duration = duration_cast<microseconds>(stop - start);

// To get the value of duration use the count()

// member function on the duration object

cout << duration.count() << endl;

|

A complete C++ program demonstrating the procedure is given below. We fill up a vector with some random numbers and measure the time taken by sort() function to sort this vector.

filter_none

brightness_4

|

// C++ program to find out execution time of

// of functions

#include <algorithm>

#include <chrono>

#include <iostream>

#include<vector>

using namespace std;

using namespace std::chrono;

// For demonstration purpose, we will fill up

// a vector with random integers and then sort

// them using sort function. We fill record

// and print the time required by sort function

int main()

{

vector<``int``> values(10000);

// Generate Random values

auto f = []() -> int { return rand``() % 10000; };

// Fill up the vector

generate(values.begin(), values.end(), f);

// Get starting timepoint

auto start = high_resolution_clock::now();

// Call the function, here sort()

sort(values.begin(), values.end());

// Get ending timepoint

auto stop = high_resolution_clock::now();

// Get duration. Substart timepoints to

// get durarion. To cast it to proper unit

// use duration cast method

auto duration = duration_cast<microseconds>(stop - start);

cout << "Time taken by function: "

<< duration.count() << " microseconds" << endl;

return 0;

}

|

Output: (Machine Dependent)

<pre style="box-sizing: border-box; margin: 0px 0px 10px; padding: 20px; border: 0px; font-size: 12pt; vertical-align: baseline; background-color: var(--gfg-grey-bg); border-radius: 10px; color: var(--color-black); font-family: Consolas, monospace; overflow: auto; white-space: pre;">Time taken by function: 3062 microseconds</pre>

相关文章

网友评论

      本文标题:Measure execution time of a func

      本文链接:https://www.haomeiwen.com/subject/fgrczktx.html