MCPcopy Create free account
hub / github.com/Berkeley-CS61B/skeleton-sp23 / Experiments

Class Experiments

lab03/src/timing/Experiments.java:11–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import java.util.List;
10
11public class Experiments {
12
13 private static void printTimingTable(TimingData data) {
14 System.out.printf("%12s %12s %12s %12s\n", "N", "time (s)", "# ops", "microsec/op");
15 System.out.println("------------------------------------------------------------");
16 for (int i = 0; i < data.getNs().size(); i += 1) {
17 int N = data.getNs().get(i);
18 double time = data.getTimes().get(i);
19 int opCount = data.getOpCounts().get(i);
20 double timePerOp = time / opCount * 1e6;
21 System.out.printf("%12d %12.2f %12d %12.2f\n", N, time, opCount, timePerOp);
22 }
23 }
24
25 /** Computes the nth Fibonacci number using a slow naive recursive strategy.*/
26 private static int fib(int n) {
27 if (n < 0) {
28 return 0;
29 }
30 if (n == 1) {
31 return 1;
32 }
33 return fib(n - 1) + fib(n - 2);
34 }
35
36 public static TimingData exampleFibonacciExperiment() {
37 List<Integer> Ns = new ArrayList<>();
38 List<Double> times = new ArrayList<>();
39 List<Integer> opCounts = new ArrayList<>();
40
41 // We're computing each fibonacci number 100 times to get a more stable number
42 int ops = 100;
43
44 for (int N = 10; N < 31; N++) {
45 Ns.add(N);
46 opCounts.add(ops);
47 Stopwatch sw = new Stopwatch();
48 for (int j = 0; j < ops; j++) {
49 int fib = fib(N);
50 }
51 times.add(sw.elapsedTime());
52 }
53
54 return new TimingData(Ns, times, opCounts);
55 }
56
57 public static TimingData timeAListConstruction() {
58 List<Integer> Ns = new ArrayList<>();
59 List<Double> times = new ArrayList<>();
60 List<Integer> opCounts = new ArrayList<>();
61
62 // TODO: YOUR CODE HERE
63
64 return null;
65 }
66
67
68 public static TimingData timeSLListGetLast() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected