The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Need some help with the C++code on Part A been trying to solve it but has been getting errors.
Part A:
We will look at timing operations in C++. To see the difference in operations, write C++ code to compare cout and printf, and display the time difference for 100 cout operations and 100 printf operations. This code can be written in Visual Studio. Below is how you would time the 100 cout operations in Visual Studio. Add another for loop, and display time for both cout and printf then comment about why you think there is a difference.
#include
#include
using namespace std;
Â
int main()
{
double start, stop;
start=clock();
for (int i=0;i{
cout
stop =clock();
cout}
Â
C++ code:
Â
Â
Screenshot of output:
Â
Â
Â
Why is one faster than the other?
Â
Â
Week 4 Lab—Optimizing Program Performance
TCO 4—Given the importance of speculating runtime costs of software, obtain an understanding of
certain details of how processors operate that impact code performance.
Scenario
In this week’s lab, you will look at timing operations and how different operations can take a
different amount of time to complete. Rubric
Point distribution for this activity
Lab Activity
Document Points
possible Part A 20 Part B 20
Total Points Points
received 40 Part A:
We will look at timing operations in C++. To see the difference in operations, write C++ code to
compare cout and printf, and display the time difference for 100 cout operations and 100 printf
operations. This code can be written in Visual Studio. Below is how you would time the 100 cout
operations in Visual Studio. Add another for loop, and display time for both cout and printf then
comment about why you think there is a difference.
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
double start, stop;
start=clock();
for (int i=0;i<100;i++)
{
cout<<" The number is "+i<<endl;
} stop =clock();
cout<<"It took "<<(double(stop-start)/CLOCKS_PER_SEC)<<" seconds for cout"<<endl;
cin.ignore();
} C++ code: Screenshot of output: Why is one faster than the other? Part B:
Next, we will take timing a step further. There are a few different sorting algorithms, and these
algorithms have varying time complexity. For this part of the lab, we will implement bubble sort and
quick sort. Research bubble sort and quick sort algorithms, and implement them in C++. Fill a large
array (maybe 15,000 elements) of random numbers. Then time the difference between bubble sort and
quick sort 10 times, and fill out the table. Next, run the program in release mode. Did you notice a
difference in time? Copy your C++ code here. Debug mode: Iteration 1
2
3
4
5
6
7
8
9
10
Release mode: Bubble sort
time Quick sort
time Bubble sort time: _________________________
Quick sort time: __________________________ Example output
-----------