Question: I need to ensure my code takes in command line arguments. Ive posted the code and command line execution below. Command line execution: java cs280a1.hw2.Memory

I need to ensure my code takes in command line arguments. Ive posted the code and command line execution below.

Command line execution: java cs280a1.hw2.Memory

package cs280a1.hw2; import java.util.Random; import java.util.*;

public class Memory { private static final int SIZE = 100000; private static final int TRIES = 1000; private static volatile int loopVar = 0; public static void main(String[] args) { long total = 0; long start, end, runTot ; // Loop without volatile keyword for (int i = 0; i < TRIES; i++) { start = System.nanoTime(); runTot = 0; for (int j = 0; j < SIZE; j++) { if (j % 2 == 0) { runTot += j; } else { runTot -= j; } } end = System.nanoTime(); total += end - start; } double avgTime = (double) total / TRIES; System.out.println("Average time without volatile keyword: " + avgTime + " nanoseconds"); total = 0; // Loop with volatile keyword for (int i = 0; i < TRIES; i++) { start = System.nanoTime(); runTot = 0; for (loopVar = 0; loopVar < SIZE; loopVar++) { if (loopVar % 2 == 0) { runTot += loopVar; } else { runTot -= loopVar; } } end = System.nanoTime(); total += end - start; } avgTime = (double) total / TRIES; System.out.println("Average time with volatile keyword: " + avgTime + " nanoseconds"); int size = 1000000; int[] arr = new int[size]; long seed = 12345; Random rand = new Random(seed); // Fill array with random numbers for (int i = 0; i < size; i++) { arr[i] = rand.nextInt(); } int experiments = 1000; int first10Percent = size / 10; int last10Percent = size - first10Percent; long startTime, endTime; long accessFirst10PercentTime = 0, accessLast10PercentTime = 0; long sum = 0; for (int i = 0; i < experiments; i++) { // Access each element in the first 10% of the array startTime = System.nanoTime(); for (int j = 0; j < first10Percent; j++) { int val = arr[j]; } endTime = System.nanoTime(); accessFirst10PercentTime += (endTime - startTime) / first10Percent; // Access a single random element in the last 10% of the array int randIndex = rand.nextInt(last10Percent) + first10Percent; startTime = System.nanoTime(); int val = arr[randIndex]; endTime = System.nanoTime(); accessLast10PercentTime += (endTime - startTime); // Calculate sum of accessed elements sum += val; } // Calculate averages double avgAccessFirst10PercentTime = (double) accessFirst10PercentTime / experiments; double avgAccessLast10PercentTime = (double) accessLast10PercentTime / experiments; double avgSum = (double) sum / experiments; // Output results System.out.println("Avg time to access known element: " + avgAccessFirst10PercentTime + " nanoseconds"); System.out.println("Avg time to access random element: " + avgAccessLast10PercentTime + " nanoseconds"); System.out.println("Sum: " + avgSum); long totalTime; // Allocate a TreeSet and fill with range [0, size) Set treeSet = new TreeSet(); for (int i = 0; i < size; i++) { treeSet.add(i); } // Allocate a LinkedList and fill with range [0, size) List linkedList = new LinkedList(); for (int i = 0; i < size; i++) { linkedList.add(i); } // Time how long .contains() method takes for TreeSet totalTime = 0; for (int i = 0; i < experiments; i++) { int randomNum = (int) (Math.random() * size); startTime = System.nanoTime(); treeSet.contains(randomNum); endTime = System.nanoTime(); totalTime += endTime - startTime; } avgTime = (double) totalTime / experiments; System.out.println("Average time for TreeSet: " + avgTime + " nanoseconds"); // Time how long .contains() method takes for LinkedList totalTime = 0; for (int i = 0; i < experiments; i++) { int randomNum = (int) (Math.random() * size); startTime = System.nanoTime(); linkedList.contains(randomNum); endTime = System.nanoTime(); totalTime += endTime - startTime; } avgTime = (double) totalTime / experiments; System.out.println("Average time for LinkedList: " + avgTime + " nanoseconds"); } }

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!