Question: 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

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<100;i++)

{

cout<<" The number is "+i<

}

stop =clock();

cout<<"It took "<<(double(stop-start)/CLOCKS_PER_SEC)<<" seconds for cout"<

cin.ignore();

}

C++ code:

Screenshot of output:

Why is one faster than the other?

Try this code in Java. Use eclipse or netbeans to see the difference in operations. Add for loops and display time for both System.out.pritnln and System.out.printf then comment about why you think there is a difference

public class optMain {

public static void main(String[] args) {

// TODO Auto-generated method stub

long start, stop=0;

start = System.nanoTime();

for (int i=0;i<100;i++)

{

System.out.println(" The number is "+i);

}

stop = System.nanoTime()-start;

System.out.println("It took "+stop+" nano seconds for code to run");

}

}

Java code:

Screenshot of output:

Why is one faster than the other?

Part B:

Next we will take timing a step further. Feel free to use C++, C#, or Java (ArrayList) for this next exercise. Determine the runtime differences between a fixed array and a dynamic array. Add several items to each array (I added 100000). A static array is created in the stack and allocated at compile time whereas a dynamic array is created in the heap and allocated at run time.

Copy your C++ or Java Code here:

Static array time: _________________________ (this may be 0)

Dynamic array time: __________________________

Explain the difference between the stack and the heap below:

Example output

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!