Question: Copy the program code presented below and save it as PerformanceTest.java. Then, complete the following: a. Compile the program and fix any syntax problem if

Copy the program code presented below and save it as PerformanceTest.java. Then, complete the following:

a. Compile the program and fix any syntax problem if theres any so that it can be executed.

b. State what sum1 and sum2 in the program try to calculate writing a math expression to show the calculation is OK.

c. State the program's functionality (what does its output mean?)

d. Run it at least 3 times and do a screenshot for each execution including the output and copy it in your answer document (your submission must have at least 3 screenshots).

e. Compare the results you received from all three runs, are the printouts the same? If not, what could be the reason for the differences?

f. The above activities can be considered as an experiment to compare the runtimes for an algorithm. Based on your observation, do you think empirical comparison is a reliable approach for comparing algorithm performances? Why or why not?

Code:

import java.lang.Math;

public class PerformanceTest{

public static void main(String [] args){

int n = 10;

int sum1, sum2;

int i;

int j;

//1st segement

for (n=1; n<= 1000000; n*=10){

sum1 = 0;

long startTime = System.nanoTime();

for (i = n; i>=1; i--)

for (j = 1; j<=100; j++)

sum1++;

long estimatedTime = System.nanoTime() - startTime;

System.out.print("[n="+ n + "]");

System.out.print("estimated time for the 1st program segment is: ");

System.out.println(estimatedTime);

}

//2nd segment

for (n=1; n<= 1000000; n*=10){

sum2 = 0;

long startTime = System.nanoTime();

for (i = 1; i< Math.pow(2,n); i=i*2)

sum2=sum2+i;

long estimatedTime = System.nanoTime() - startTime;

System.out.print("[n="+ n + "]");

System.out.print("estimated time for the 2nd program segment is: ");

System.out.println(estimatedTime);

}

}

}

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!