Question: In C++ Task The need arises for one to perform benchmarking of code (i.e., time how long something takes to execute). Such is useful enough

In C++

Task

The need arises for one to perform "benchmarking" of code (i.e., time how long something takes to execute). Such is useful enough to place the code to do such inside a function (template) so it can be easily used again later.

main()

The program's main() function has only one function call in it to the benchmark() function you will write later in this assignment. The benchmark() function has a single argument: a lambda function (no arguments, void return) whose body is:

int count = 0; for (double d; cin >> d; ++count) ; cout << count << " double values were read in. "; 

The benchmark() Function Template

Start by writing the function prototype followed by the opening {, i.e.,

template  void benchmark(Func f) { 

Now you must:

  1. Record the start time, t0, by calling std::chrono::steady_clock::now().
  2. Call the function that f refers to.
  3. Record the stop time, t1, by calling std::chrono::steady_clock::now().
  4. Determine the amount of time (as a double) in seconds that elapsed by calling std::chrono::duration
  5. Output "benchmark() took
  6. Close the function definition. (Obviously!)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!