Browse Source

add reverse, rotate, and tests for both.

Charles Reid 2 years ago
parent
commit
4153077992
1 changed files with 103 additions and 7 deletions
  1. 103
    7
      lists/linked-lists/Timing.java

+ 103
- 7
lists/linked-lists/Timing.java View File

@@ -14,8 +14,100 @@ public class Timing {
14 14
 
15 15
 	// Tests
16 16
 	public static void main(String[] args) { 
17
-		linked_list_add_test();
18
-		linked_list_add_remove_test();
17
+		//linked_list_add_test();
18
+		//linked_list_add_remove_test();
19
+		//test_reverse();
20
+		test_rotate();
21
+	}
22
+
23
+
24
+
25
+
26
+	/** Verify rotate linked list method is O(n). */
27
+	public static void test_rotate() {
28
+
29
+		StringBuffer sb = new StringBuffer();
30
+		sb.append("N, Walltime Rotate (ms), Amortized Rotate (us)\n");
31
+
32
+		int ntrials = 1000;
33
+		Random r;
34
+
35
+		// Loop over values of N
36
+		for(int N = (int)(5E3); N <= (int)(5E5); N+=2500) { 
37
+
38
+			Tim tim = new Tim();
39
+
40
+			// Trials counter is always k for Kafka
41
+			for(int k = 0; k<ntrials; k++) { 
42
+				// Each trial is a different sequence of random numbers,
43
+				// but the sequence matches between tests of different collection types 
44
+				r = new Random(k);
45
+				TLinkedList<Integer> mylist = new TLinkedList<Integer>();
46
+        		for(int i=0; i<N; i++) { 
47
+					mylist.addFirst(r.nextInt(100));
48
+				}
49
+				mylist.rotate();
50
+			}
51
+			double time_total = tim.elapsedms();
52
+
53
+			// print amortized cost per 1000 operations, times 1000 ms -> us
54
+			sb.append( String.format("%d, ",N) );
55
+			sb.append( String.format("%.3f, ", time_total) );
56
+			sb.append( String.format("%.3f ", time_total/N*1000) );
57
+			sb.append("\n");
58
+		}
59
+
60
+		System.out.println(sb.toString());
61
+	}
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+	/** Verify reverse linked list method is O(n). */
73
+	public static void test_reverse() {
74
+
75
+		StringBuffer sb = new StringBuffer();
76
+		sb.append("N, Walltime Reverse (ms), Amortized Reverse (us)\n");
77
+
78
+		int ntrials = 1000;
79
+		Random r;
80
+
81
+		// Loop over values of N
82
+		for(int N = (int)(1E3); N <= (int)(1E5); N+=2500) { 
83
+
84
+
85
+			// builtin linked list:
86
+			//
87
+			// b prefix is for built-in datatype
88
+			Tim tim = new Tim();
89
+
90
+			// Trials counter is always k for Kafka
91
+			for(int k = 0; k<ntrials; k++) { 
92
+				// Each trial is a different sequence of random numbers,
93
+				// but the sequence matches between tests of different collection types 
94
+				r = new Random(k);
95
+				TLinkedList<Integer> mylist = new TLinkedList<Integer>();
96
+        		for(int i=0; i<N; i++) { 
97
+					mylist.addFirst(r.nextInt(100));
98
+				}
99
+				mylist.reverse();
100
+			}
101
+			double time_total = tim.elapsedms();
102
+
103
+			// print amortized cost per 1000 operations, times 1000 ms -> us
104
+			sb.append( String.format("%d, ",N) );
105
+			sb.append( String.format("%.3f, ", time_total) );
106
+			sb.append( String.format("%.3f ", time_total/N*1000) );
107
+			sb.append("\n");
108
+		}
109
+
110
+		System.out.println(sb.toString());
19 111
 	}
20 112
 
21 113
 
@@ -30,7 +122,10 @@ public class Timing {
30 122
 	public static void linked_list_add_remove_test() {
31 123
 		StringBuffer sb = new StringBuffer();
32 124
 
33
-		sb.append("N, Add Remove Amortized Walltime Per 1k Builtin (us), Add Remove Amortized Walltime Per 1k SLL (us), Add Remove Amortized Walltime Per 1k DLL (us) \n");
125
+		//
126
+		// Add Remove Amortized Walltime Per 1k Builtin (us), Add Remove Amortized Walltime Per 1k SLL (us), Add Remove Amortized Walltime Per 1k DLL (us)
127
+		//
128
+		sb.append("N, Walltime Builtin (us), Walltime SLL (us), Walltime DLL (us)\n");
34 129
 		
35 130
 		int ntrials = 1000;
36 131
 		Random r;
@@ -118,7 +213,7 @@ public class Timing {
118 213
 
119 214
 			// print amortized cost per 1000 operations, times 1000 ms -> us
120 215
 			sb.append( String.format("%.3f, ", btime_total/N*1000) );
121
-			sb.append( String.format("%.3f, ",  mtime_total/N*1000) );
216
+			sb.append( String.format("%.3f, ", mtime_total/N*1000) );
122 217
 			sb.append( String.format("%.3f ",  dtime_total/N*1000) );
123 218
 
124 219
 
@@ -143,7 +238,8 @@ public class Timing {
143 238
 
144 239
 		StringBuffer sb = new StringBuffer();
145 240
 
146
-		sb.append("N, Add Amortized Walltime Per 1k Builtin (us), Add Amortized Walltime Per 1k SLL (us), Add Amortized Walltime Per 1k DLL (us) \n");
241
+		// Add Amortized Walltime Per 1k Builtin (us), Add Amortized Walltime Per 1k SLL (us)
242
+		sb.append("N, Add Builtin (us), Add SLL (us), Add DLL (us)\n");
147 243
 		
148 244
 		int ntrials = 1000;
149 245
 
@@ -203,8 +299,8 @@ public class Timing {
203 299
 
204 300
 			// print cost per 1000 add operations us, times 100 ms -> us
205 301
 			sb.append( String.format("%.3f, ",  btime_total/N*1000) );
206
-			sb.append( String.format("%.3f, ",    mtime_total/N*1000) );
207
-			sb.append( String.format("%.3f ",    btime_total/N*1000) );
302
+			sb.append( String.format("%.3f, ",  mtime_total/N*1000) );
303
+			sb.append( String.format("%.3f ",   dtime_total/N*1000) );
208 304
 			sb.append("\n");
209 305
 
210 306
 		}