Question
c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i =
c++ help
Write a program that obtains the execution times for the following three code segments with the different n.
code 1:
for(int i = 0; i <= n; i++)
{
x = i;
}
code 2:
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
x = i + j;
}
}
code 3:
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k <= n; k++)
{
x = i + j + k;
}
}
}
Please record the execution time of the following tasks, Code 1 when n=100, n=1,000, n=5,000, n=10,000, n=100,000, n=1,000,000, n=2,000,000, and n=10,000,000 Code 2 when n=100, n=1,000, n=5,000, n=10,000, n=100,000, and n=200,000. Code 3 when n=10, n=50, n=100, n=500, and n = 1,000. You can fill a table like this:
n=10 | n=50 | n=100 | n=200| n=1000|
Code 1 | | | | |
Code 2 | | | | |
Code 3 | | | | | Plotting the execution time of Code 1, Code 2 and Code 3. You may want to execute Code1, Code2 and Code 3 with more n values to plot. You can use the following code template to obtain the execution time.
#include
#include
#include
{
using clock = chrono::steady_clock;
clock::time_point start = clock::now();//Get start time
// perform the task Code 1 which you want to analyze the
// execution time
int x = 0;
for(int i = 0; i <= n; i++)
{
x = i;
}
clock::time_point end = clock::now(); //Get end time clock::duration time_span = end - start; double ns = double(time_span.count()) * clock::period::num /
clock::period::den;
cout << "Code 1 execution time for n = " << n
<< " is " << ns << " seconds" << endl; } You can put all functions in the file a1q10.cpp. Please submit the program a1q10.cpp, a script file a1q10result containing result, and the plot of execution time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started