From ea3b1b4b63efaf45c6043729671af9d2327ed75f Mon Sep 17 00:00:00 2001 From: Charles Reid Date: Sun, 4 Jun 2017 21:32:07 -0700 Subject: [PATCH] Fixing up timing, prints out 1e3 to 1e6 scaled linked list performance. --- lists/linked-lists/Timing.java | 171 ++++++++++++++++++++++++++++++--- 1 file changed, 155 insertions(+), 16 deletions(-) diff --git a/lists/linked-lists/Timing.java b/lists/linked-lists/Timing.java index b632b5c..0455e1c 100644 --- a/lists/linked-lists/Timing.java +++ b/lists/linked-lists/Timing.java @@ -1,4 +1,5 @@ import java.util.LinkedList; +import java.util.Random; /** Timing class: measure big-O complexity and runtime of data structures. * @@ -14,26 +15,142 @@ public class Timing { // Tests public static void main(String[] args) { linked_list_add_test(); + linked_list_add_remove_test(); + } + + + + + + /** Compare the add and remove method - adding and removing from list. + * + * NOTE: If this is not constant, then add is not O(1) amortized, + * and there is something wrong with your implementation. + * */ + public static void linked_list_add_remove_test() { + StringBuffer sb = new StringBuffer(); + + 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"); + + int ntrials = 1000; + Random r; + + // Loop over values of N + for(int N = (int)(1E3); N <= (int)(1E6); N*=4) { + + sb.append( String.format("%d, ",N) ); + + + // builtin linked list: + // + // b prefix is for built-in datatype + Tim btim = new Tim(); + + // Trials counter is always k for Kafka + for(int k = 0; k blist = new LinkedList(); + for(int i=0; i mylist = new TLinkedList(); + for(int i=0; i dlist = new TLinkedList(); + for(int i=0; i us + sb.append( String.format("%.3f, ", btime_total/N*1000) ); + sb.append( String.format("%.3f, ", mtime_total/N*1000) ); + sb.append( String.format("%.3f ", dtime_total/N*1000) ); + + + sb.append("\n"); + + } + + + System.out.println(sb.toString()); + } /** Compare the add method - appending to the rear of a list. * * Compares builtin LinkedList type with self-authored TLinkedList class. + * + * NOTE: If this is not constant, then add is not O(1) amortized, + * and there is something wrong with your implementation. * */ public static void linked_list_add_test() { + StringBuffer sb = new StringBuffer(); - System.out.println("N, Walltime Builtin (s), Walltime Mine (s)"); + 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"); + + int ntrials = 1000; - // Loop over some values of N, and print the amortized operational cost. - for(int N = 1024; N < 1E6; N*=2) { - - int ntrials = 1000; - - // Hold on to your butts. - - System.out.printf("%d, ", N); + // Loop over some values of N, + // print the total cost, + // print the cost per add operation + for(int N = 1024; N < 5e5; N *= 2) { // builtin linked list: @@ -49,12 +166,9 @@ public class Timing { blist.add(i*i); } } - double btime_total = btim.elapsed(); - - System.out.printf("%.2f,", btime_total); - + double btime_total = btim.elapsedms(); - // my little linked list: + // singly linked list: // // m prefix is for mine Tim mtim = new Tim(); @@ -66,11 +180,36 @@ public class Timing { mylist.addLast(i*i); } } - double mtime_total = mtim.elapsed(); - System.out.printf("%.2f \n", mtime_total); + double mtime_total = mtim.elapsedms(); + // doubly linked list: + Tim dtim = new Tim(); + + // Trials counter is k + for(int k=0; k dlist = new TLinkedList(); + for(int i=0; i us + sb.append( String.format("%.3f, ", btime_total/N*1000) ); + sb.append( String.format("%.3f, ", mtime_total/N*1000) ); + sb.append( String.format("%.3f ", btime_total/N*1000) ); + sb.append("\n"); + } + + System.out.println(sb.toString()); }