Dont Copy From Others, I Will Give You Thumb Up If You Follow All Requirement. Thank You!
Question:
Dont Copy From Others, I will give you thumb up if you follow all requirement. Thank You!
- write a function template,
- the C++ std::steady_clock type to record the "time" in order to determine how long something took, and,
- use a lambda function (that is passed to the function template which calls the function).
- 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
The benchmark() Function Template
Start by writing the function prototype followed by the opening {, i.e.,
template
void benchmark(Func f)
{
Now you must:
- Record the start time, t0, by calling std::chrono::steady_clock::now().
- Call the function that f refers to.
- Record the stop time, t1, by calling std::chrono::steady_clock::now().
- Determine the amount of time (as a double) in seconds that elapsed by calling std::chrono::duration(t1-t0).count().
- Output "benchmark() took seconds" (without the quotes) followed by a newline. (Replace with the time computed in the previous step.)
- Close the function definition. (Obviously!)
Sample Program Run(output)
An example compilation and execution of a valid solution to this assignment could be:
$ g++ -std=c++20 -Wall -Wextra a03.cpp
$ ./a.out
1.1 1e-12 1.2e34 -93.23
4 double values were read in.
benchmark() took 28.4159 seconds
$