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: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Would like help with Part B of this lab. Only need part B.
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 |
Points received |
|
Part A |
20 |
 |
|
Part B |
20 |
 |
|
Total Points |
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>
usingnamespace std;
Â
int main()
{
      double start, stop;
      start=clock();
      for (inti=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?
Â
Â
Â
Â
Â
Â
Debug mode:
Â
|
Iteration |
Bubble sort time |
Quick sort time |
|
1 |
 |
 |
|
2 |
 |
 |
|
3 |
 |
 |
|
4 |
 |
 |
|
5 |
 |
 |
|
6 |
 |
 |
|
7 |
 |
 |
|
8 |
 |
 |
|
9 |
 |
 |
|
10 |
 |
 |
Release mode:
Bubble sort time: _________________________
Quick sort time: __________________________
Â
Â
Â
Â
Example output
Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly