Question

In: Computer Science

Perform an experimental analysis to test and compares the relative running times of the methods shown...

Perform an experimental analysis to test and compares the relative running times of the methods shown in the following Code Fragments. Calculate the execution time of each example in milliseconds and draw the results in one chart using line chart type. [HINT: use the following five different inputs size to check each example (1000, 10000, 100000, 1000000, 10000000). You can generate each array using random number generator to fill the array with the pervious input size].

/** Returns the sum of the integers in given array. */ public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } /** Returns the sum of the integers with even index in given array. */ public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return total; } /** Returns the sum of the prefix sums of given array. */ public static int example3(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 for (int k=0; k <= j; k++) // loop from 0 to j total += arr[j]; return total; } /** Returns the sum of the prefix sums of given array. */ public static int example4(int[] arr) { int n = arr.length, prefix = 0, total = 0; for (int j=0; j < n; j++) { // loop from 0 to n-1 prefix += arr[j]; total += prefix; } return total; } }

Solutions

Expert Solution

Input array size on x axis and execution time on y axis.

Code used for the testing:

package test;

public class Experiment {
   public static void main(String[] args){
       int[] arr;
       long time_start;
       long time_end;
       int arr_size = 100;
      
       for(int i = 0; i < 5; i++){
           /*
           * calculating array size for current run
           */
           arr_size *= 10;
           /*
           * getting array of andom numbers for current run
           */
           arr = getRandomArray(arr_size);
          
           System.out.println("First round run with array size: " + arr_size);          
           System.out.println("\tRunning function example1: ");
           /*
           * getting current system time before execution
           */
           time_start = System.currentTimeMillis();
           System.out.println("\t\tStarting time: " + time_start);
           /*
           * execution of example1 function
           */
           Experiment.example1(arr);
           /*
           * getting current system time after execution
           */
           time_end = System.currentTimeMillis();
           System.out.println("\t\tEnding time: " + time_end);
           System.out.println("\t\tExecution time: " + (time_end - time_start));
                  
          
          
           System.out.println("\tRunning function example2: ");
           /*
           * getting current system time before execution
           */
           time_start = System.currentTimeMillis();
           System.out.println("\t\tStarting time: " + time_start);
           /*
           * execution of example2 function
           */
           Experiment.example2(arr);
           /*
           * getting current system time after execution
           */
           time_end = System.currentTimeMillis();
           System.out.println("\t\tEnding time: " + time_end);
           System.out.println("\t\tExecution time: " + (time_end - time_start));
          
          
          
           System.out.println("\tRunning function example3: ");
           /*
           * getting current system time before execution
           */
           time_start = System.currentTimeMillis();
           System.out.println("\t\tStarting time: " + time_start);
           /*
           * execution of example3 function
           */
           Experiment.example3(arr);
           /*
           * getting current system time after execution
           */
           time_end = System.currentTimeMillis();
           System.out.println("\t\tEnding time: " + time_end);
           System.out.println("\t\tExecution time: " + (time_end - time_start));
          
          
          
           System.out.println("\tRunning function example4: ");
           /*
           * getting current system time before execution
           */
           time_start = System.currentTimeMillis();
           System.out.println("\t\tStarting time: " + time_start);
           /*
           * execution of example4 function
           */
           Experiment.example4(arr);
           /*
           * getting current system time after execution
           */
           time_end = System.currentTimeMillis();
           System.out.println("\t\tEnding time: " + time_end);
           System.out.println("\t\tExecution time: " + (time_end - time_start));
  
       }
   }
  
   public static int[] getRandomArray(int length){
       int[] arr = new int[length];
       for(int i = 0; i < length; i++){
           arr[i] = (int) Math.random() * 100;
       }
       return arr;
   }
  
   /** Returns the sum of the integers in given array. */
   public static int example1(int[] arr) {
       int n = arr.length, total = 0;
       for (int j=0; j < n; j++) // loop from 0 to n-1
           total += arr[j]; return total;
   }
  
   /** Returns the sum of the integers with even index in given array. */
   public static int example2(int[] arr) {
       int n = arr.length, total = 0;
       for (int j=0; j < n; j += 2) // note the increment of 2
           total += arr[j];
       return total;
   }
  
   /** Returns the sum of the prefix sums of given array. */
   public static int example3(int[] arr) {
       int n = arr.length, total = 0;
       for (int j=0; j < n; j++) {// loop from 0 to n-1
           for (int k=0; k <= j; k++) // loop from 0 to j
               total += arr[j];
       }
       return total;
   }
  
   /** Returns the sum of the prefix sums of given array. */
   public static int example4(int[] arr) {
       int n = arr.length, prefix = 0, total = 0;
       for (int j=0; j < n; j++) { // loop from 0 to n-1
           prefix += arr[j];
           total += prefix;
       }
       return total;
   }
}

Code screenshot:

Output of execution:


Related Solutions

Perform an experimental analysis to test and compares the relative running times of this method [HINT:...
Perform an experimental analysis to test and compares the relative running times of this method [HINT: use the following inputs size to check each example (1000). You can generate each array using random number generator to fill the array with the pervious input size]. /** Returns the sum of the integers in given array. */ public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to...
The most common experimental technique to perform elemental analysis is combustion analysis, where a sample is...
The most common experimental technique to perform elemental analysis is combustion analysis, where a sample is burned in a large excess of oxygen and the combustion products are trapped in a variety of ways. A 99.99% pure, 0.4831 g sample containing only carbon, hydrogen, and nitrogen is subjected to combustion analysis, resulting in the formation of 1.353 g CO2, 0.2750 g H2O, and 0.1833 g NO. What is the empirical formula of the sample?
Describe two useful methods to “perform quantitative analysis
Describe two useful methods to “perform quantitative analysis
The weights in ounces of sample running shoes for men and women are shown. Test the...
The weights in ounces of sample running shoes for men and women are shown. Test the claim that the means are different. Use the p-value method with a=0.05 men: 10.4, 11.1, 10.8, 11.7, 12.8, 12.6, 14.7, 12.9, 13.3, 14.5 women: 10.6, 9.6, 10.1, 9.4, 9.8, 10.2, 9.5, 11.2, 10.3, 10.3, 8.8, 9.5, 9.3, 9.5, 11.0
1. Outline two experimental methods to test the hypothesis that marijuana alters a) gene expression and...
1. Outline two experimental methods to test the hypothesis that marijuana alters a) gene expression and b) protein expression in mice brains. Explain also how you will know the specific identity of the genes and proteins that are altered. You will need to search the literature for biochemical techniques that would be appropriate. 2. Assume you find a gene that is upregulated in mice brains in response to marijuana. How would you determine exactly where in the mouse brain this...
explain how experimental design, analysis of variance, and chi square test are used in research.
explain how experimental design, analysis of variance, and chi square test are used in research.
At least three times a day during this week, perform an S-O-R black box analysis on...
At least three times a day during this week, perform an S-O-R black box analysis on yourself. Observe what you are doing (especially in terms of the products and services you are using or purchasing). Identify the role of the stimuli and organismic background factors in influencing these behaviors and decisions.
Quick strategy analysis(100 words max) for New York Times using one of the strategy methods, for...
Quick strategy analysis(100 words max) for New York Times using one of the strategy methods, for examples ( CARTS:  • Crisis Perception • Ambidexterity • Reorganization • Targets • Scenario Analysis
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a....
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a. Organize the data and show in MS Excel (5 points); b. Write down one potential question that you could answers using Chi-square test with the Happiness_2011.xls dataset  and state its null and alternate hypotheses (5 points); c. Perform one Nonparametric Methods: Chi-Square Test using any two reasonable variables from the Happiness_2011.xls dataset (two qualitative variables) and show the analysis results for the question (10 points);...
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a....
Please Perform one Chi-square Test by doing the following (Hint: Chapter 15/17, Nonparametric Methods: Chi-Square): a. Organize the data and show in MS Excel (5 points); b. Write down one potential question that you could answers using Chi-square test with the Happiness_2011.xls dataset and state its null and alternate hypotheses (5 points); c. Perform one Nonparametric Methods: Chi-Square Test using any two reasonable variables from the Happiness_2011.xls dataset (two qualitative variables) and show the analysis results for the question (10...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT