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

Class InsertInOrderSpeedTest

lab07/tests/InsertInOrderSpeedTest.java:14–137  ·  view source on GitHub ↗

Performs a timing test on three different set implementations. For BSTMap purposes assumes that are pairs. @author Josh Hug @author Brendan Hu

Source from the content-addressed store, hash-verified

12 * @author Brendan Hu
13 */
14public class InsertInOrderSpeedTest {
15 /**
16 * Requests user input and performs tests of three different set
17 * implementations. ARGS is unused.
18 */
19 public static void main(String[] args) {
20 Scanner input = new Scanner(System.in);
21
22 // borrow waitForPositiveInt(Scanner input) from InsertRandomSpeedTest
23 System.out.println("This program inserts lexicographically "
24 + "increasing Strings into Maps as <String, Integer> pairs.");
25
26 String repeat;
27 do {
28 System.out.print("\nEnter # strings to insert into the maps: ");
29 int N = InsertRandomSpeedTest.waitForPositiveInt(input);
30 timeInOrderMap61B(new ULLMap<>(), N);
31 timeInOrderMap61B(new BSTMap<>(), N);
32 timeInOrderTreeMap(new TreeMap<>(), N);
33 timeInOrderHashMap(new HashMap<>(), N);
34
35 System.out.print("Would you like to try more timed-tests? (y/n): ");
36 repeat = input.nextLine();
37 } while (!repeat.equalsIgnoreCase("n") && !repeat.equalsIgnoreCase("no"));
38 input.close();
39 }
40
41 /**
42 * Returns time needed to put N strings into a Map61B in increasing order.
43 * makes use of StringUtils.nextString(String s)
44 */
45 public static double insertInOrder(Map61B<String, Integer> map61B, int N) {
46 Stopwatch sw = new Stopwatch();
47 String s = "cat";
48 for (int i = 0; i < N; i++) {
49 s = StringUtils.nextString(s);
50 map61B.put(s, i);
51 }
52 return sw.elapsedTime();
53 }
54
55 /**
56 * Returns time needed to put N strings into TreeMap in increasing order.
57 */
58 public static double insertInOrder(TreeMap<String, Integer> ts, int N) {
59 Stopwatch sw = new Stopwatch();
60 String s = "cat";
61 for (int i = 0; i < N; i++) {
62 s = StringUtils.nextString(s);
63 ts.put(s, i);
64 }
65 return sw.elapsedTime();
66 }
67
68 public static double insertInOrder(HashMap<String, Integer> ts, int N) {
69 Stopwatch sw = new Stopwatch();
70 String s = "cat";
71 for (int i = 0; i < N; i++) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected