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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
