Introduction
- Due 31 Aug 2022 by 18:00
- Points 2
- Submitting a file upload
- File types pdf
- Available 29 Aug 2022 at 8:00 - 2 Sep 2022 at 23:59
A soft start to see that you can measure timeand write a nice reprort. The report should be written using LaTeX and you do so either (recommended) by installing LaTeX locally on you laptop or, by using Overleaf. Links to an external site. The advantage with Overleaf is that you will be up and running in five minutes and you see the result immediately. The down side is that you will not be able to use your regular editor and will have a some more work when integrating graphs etc.
Proposal how to solve the first task:
import java.util.Random; class Access { private static int access(int[] arr, int[] indx) { int loop = indx.length; int sum = 0; for (int i = 0; i < loop; i++) { sum += arr[indx[i]]; } return sum; } private static void time(int tries, int loop, int n) { Random rnd = new Random(); int[] indx = new int[loop]; for (int i = 0; i < loop ; i++) { indx[i] = rnd.nextInt(n); } int[] arr = new int[n]; for (int i = 0; i < n ; i++) { arr[i] = i; } double min = Double.POSITIVE_INFINITY; double max = 0; double sum = 0; for (int i = 0; i < tries; i++) { double n0 = System.nanoTime(); access(arr, indx); double n1 = System.nanoTime(); double t = (n1 - n0); if ( t < min ) min = t; if ( t > max ) max = t; sum += t; } System.out.printf("n: %6d \t min: %.2fns\t avg: %.2fns\t max: %.2fns\n", n, (min/(loop)), (sum/(tries*loop)), (max/(loop))); } public static void main(String[] arg) { int[] sizes = {200,400,800,1600,3200,6400,12800,25600}; for ( int n : sizes) { time(10000, 100000, n); } } }