Question

In: Computer Science

For the following program segments, write a program that shows the estimated runtime for each piece....

For the following program segments, write a program that shows the estimated runtime for each piece. Run it on your computer when n=1, 10, 100, 1000, 10000, 100000, 1000000 for ten times each so that you can observe the differences in performance among these segments.

Segment1:        for (sum=0, i=1; i<=n; i++)

                        sum = sum + i;

Segment2:        for (sum=0, i=1; i<=n; i++)

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

                                                            sum++;

Segment3:        sum= n * (n+1)/2

Solutions

Expert Solution

public class ShowTimes {

        public static long sum1(int n) {
                long start = System.nanoTime();
                int sum, i;
                for (sum = 0, i = 1; i <= n; i++)
                        sum = sum + i;
                long end = System.nanoTime();

                return (end - start);
        }

        public static long sum2(int n) {
                long start = System.nanoTime();
                int sum, i, j;
                for (sum = 0, i = 1; i <= n; i++)
                        for (j = 1; j <= i; j++)
                                sum++;
                long end = System.nanoTime();

                return (end - start);
        }

        public static long sum3(int n) {
                long start = System.nanoTime();
                int sum = n * (n + 1) / 2;
                long end = System.nanoTime();

                return (end - start);
        }

        public static void main(String[] args) {

                int sizes[] = new int[] { 1, 10, 100, 1000, 10000, 100000, 1000000 };

                System.out.printf("%-15s%-10s%-10s%-10s\n", "InputSize", "Segment1", "Segment2", "Segment3");

                for (int size : sizes) {
                        System.out.printf("%-15d%-10d%-10d%-10d\n", size, sum1(size), sum2(size), sum3(size));

                }

        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using...
(Full Program)Write code that shows how deadlocks work. Then write code that shows a fix using semaphores. (Full program)Write code showing the elevator algorithm. c++ Language
For the following program descriptions, write step by step pseudo code that shows you understand the...
For the following program descriptions, write step by step pseudo code that shows you understand the problem and what it takes to solve it. The first one is done for you as an example. Please answer the questions in the same format as the example problem below so it is the same. Example #1 Problem A customer is purchasing five items. Design a program where you collect the amount of each item, calculate the subTotal of the items, the tax...
The runtime of a program is proportional to n1.5 where n is the input size.  For input...
The runtime of a program is proportional to n1.5 where n is the input size.  For input size 100 the runtime is 51 ms.  What is the new runtime when the input size is quadrupled?
Write a lex program that can identify each of the following tokens. The program should produce...
Write a lex program that can identify each of the following tokens. The program should produce a line for each token where the first word is token itself followed by the token type. • Identifiers: name of variables and functions (identifier name should be started by a letter followed by letters or digits and maximum eight characters in length). • Keywords: if, then, else, for, while, do, switch, case etc. • Numbers: integer, float (float number format that supports in...
Suppose the runtime of a computer program is proportional to 2^n where n is the input...
Suppose the runtime of a computer program is proportional to 2^n where n is the input size. When given an input of size 1024, suppose the runtime is 256ms. For what input size would the program have runtime of 1ms? A program takes time proportional to the input size. If the program takes 36 milliseconds for input size 30, how  many milliseconds does it take for input size 10? A program takes time T(n) = k2n  where k is some constant and...
Scala: Write and test a Program as elegantly as possible Given a piece of text, create...
Scala: Write and test a Program as elegantly as possible Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”, the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1 Sample Input/Output: Enter text: this is...
Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
Must be in Java Write a program called showTwos that shows the factors of 2 in...
Must be in Java Write a program called showTwos that shows the factors of 2 in a given positive integer (user input) in the range of [16,128]. For example, if user inputs 7, 18, 68, 120, the correctly working program should print the following: 7 = 7 18 = 2 * 9 68 = 2 * 2 * 17 120 = 2 * 2 * 2 * 15
write about chase bank as a whole, Growth of  Segments :    Are there segments within the industry...
write about chase bank as a whole, Growth of  Segments :    Are there segments within the industry growing faster than others?   If so, threats of entry from other segments may be high.
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT