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

Class InsertRandomSpeedTest

lab07/tests/InsertRandomSpeedTest.java:10–156  ·  view source on GitHub ↗

Performs a timing test on three different set implementations. @author Josh Hug @author Brendan Hu

Source from the content-addressed store, hash-verified

8 * @author Brendan Hu
9 */
10public class InsertRandomSpeedTest {
11 /**
12 Requests user input and performs tests of three different set
13 implementations. ARGS is unused.
14 */
15 public static void main(String[] args) {
16 Scanner input = new Scanner(System.in);
17
18 System.out.println("This program inserts random "
19 + "Strings of length L "
20 + "into different types of maps "
21 + "as <String, Integer> pairs.");
22 System.out.print("Please enter desired length of each string: ");
23 int L = waitForPositiveInt(input);
24
25 String repeat;
26 do {
27 System.out.print("\nEnter # strings to insert into the maps: ");
28 int N = waitForPositiveInt(input);
29 timeRandomMap61B(new ULLMap<>(), N, L);
30 timeRandomMap61B(new BSTMap<>(), N, L);
31 timeRandomTreeMap(new TreeMap<>(), N, L);
32 timeRandomHashMap(new HashMap<>(), N, L);
33
34 System.out.print("Would you like to try more timed-tests? (y/n)");
35 repeat = input.nextLine();
36 } while (!repeat.equalsIgnoreCase("n") && !repeat.equalsIgnoreCase("no"));
37 input.close();
38 }
39
40 /** Returns time needed to put N random strings of length L into the
41 * Map61B 61bMap. */
42 public static double insertRandom(Map61B<String, Integer> map61B, int N, int L) {
43 Stopwatch sw = new Stopwatch();
44 String s;
45 for (int i = 0; i < N; i++) {
46 s = StringUtils.randomString(L);
47 map61B.put(s, i);
48 }
49 return sw.elapsedTime();
50 }
51
52 /** Returns time needed to put N random strings of length L into the
53 * given TreeMap. */
54 public static double insertRandom(TreeMap<String, Integer> treeMap, int N, int L) {
55 Stopwatch sw = new Stopwatch();
56 String s;
57 for (int i = 0; i < N; i++) {
58 s = StringUtils.randomString(L);
59 treeMap.put(s, i);
60 }
61 return sw.elapsedTime();
62 }
63
64 /** Returns time needed to put N random strings of length L into the
65 * HashMap treeMap. */
66 public static double insertRandom(HashMap<String, Integer> treeMap, int N, int L) {
67 Stopwatch sw = new Stopwatch();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected