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

Do a walk-through of the following program segments and determine, in each case, the exact output:...
Do a walk-through of the following program segments and determine, in each case, the exact output: a) int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= 5; j++) cout << setw (3) << i; cout << endl; } b) int i, j; for (i = 1; i <= 5; i++) { for (j = (i + 1; j <= 5; j++) cout << setw(5) << j; cout << endl; c) int...
Write a C++ main program that has the following 5 short independent segments. 6. Show the...
Write a C++ main program that has the following 5 short independent segments. 6. Show the results of the following power function: pow(-2., 3), pow(-2., 3.0) , pow(-2., 3.00000000001) 7. Show the memory size of the following constants 1. , 1.F, 1 , '1' , and "1" 8. Display 1./3. using 20 digits and show the correct and incorrect digits 9. Display all printable characters of the ASCII table in 3 columns: first column: 32-63, second column: 64-95, third column:...
Write a C++ main program that has the following 5 short independent segments. 1. Show that...
Write a C++ main program that has the following 5 short independent segments. 1. Show that for unsigned int a,b and a>0, b>0, we can get a+b < a 2. Show that for int a,b and a>0, b>0, we can get a+b < 0 3. Show that for int a,b and a 0 4. Show that for double x and x>0 we can get 1. + x = = 1. 5. Show that for double a,b,c in some cases (a+b)+c...
i need to find complexity and cost and runtime for each line of the following c++...
i need to find complexity and cost and runtime for each line of the following c++ code : // A C++ program for Dijkstra's single source shortest path algorithm. // The program is for adjacency matrix representation of the graph #include <limits.h> #include <stdio.h> // Number of vertices in the graph #define V 9 // A utility function to find the vertex with minimum distance value, from // the set of vertices not yet included in shortest path tree int...
(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...
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...
Need the following program in Python: Create a program that calculates the estimated duration of a...
Need the following program in Python: Create a program that calculates the estimated duration of a trip in hours and minutes. This should include an estimated date/time of departure and an estimated date/time of arrival. Arrival Time Estimator Enter date of departure (YYYY-MM-DD): 2016-11-23 Enter time of departure (HH:MM AM/PM): 10:30 AM Enter miles: 200 Enter miles per hour: 65 Estimated travel time Hours: 3 Minutes: 5 Estimated date of arrival: 2016-11-23 Estimated time of arrival: 01:35 PM Continue? (y/n):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT