Advertisement
Guest User

Alternative benchmark to pastebin.com/5pyPMJav

a guest
Sep 2nd, 2010
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 9.37 KB | None | 0 0
  1. /* see http://stackoverflow.com/questions/3607593/is-it-faster-to-add-to-a-collection-then-sort-it-or-add-to-a-sorted-collection */
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.PriorityQueue;
  10. import java.util.Random;
  11. import java.util.TreeSet;
  12.  
  13. import org.apache.commons.lang.builder.CompareToBuilder;
  14. import org.apache.commons.lang.builder.EqualsBuilder;
  15. import org.apache.commons.lang.builder.HashCodeBuilder;
  16.  
  17. public class TestSort4 {
  18.  
  19.     public static class Thingy implements Comparable<Thingy>{
  20.  
  21.         public Thingy(final double part1, final double part2, final double part3){
  22.             this.part1 = part1;
  23.             this.part2 = part2;
  24.             this.part3 = part3;
  25.         }
  26.  
  27.         /**
  28.          * {@inheritDoc}
  29.          */
  30.         @Override
  31.         public int hashCode(){
  32.             return new HashCodeBuilder() //
  33.             .append(this.part1)
  34.                 .append(this.part2)
  35.                 .append(this.part3)
  36.                 .toHashCode();
  37.         }
  38.  
  39.         /**
  40.          * {@inheritDoc}
  41.          */
  42.         @Override
  43.         public boolean equals(final Object obj){
  44.             boolean result;
  45.             if(obj instanceof Thingy){
  46.                 final Thingy other = (Thingy) obj;
  47.                 result = new EqualsBuilder() //
  48.                 .append(this.part1, other.part1)
  49.                     .append(this.part2, other.part2)
  50.                     .append(this.part3, other.part3)
  51.                     .isEquals();
  52.             } else{
  53.                 result = false;
  54.             }
  55.             return result;
  56.         }
  57.  
  58.         private final double part1;
  59.  
  60.         private final double part2;
  61.  
  62.         private final double part3;
  63.  
  64.         @Override
  65.         public int compareTo(final Thingy other){
  66.  
  67.             return
  68.  
  69.             new CompareToBuilder().append(calcComp(this), calcComp(other))
  70.                 .toComparison();
  71.         }
  72.  
  73.         private static double calcComp(final Thingy th){
  74.             return Math.pow(th.part1 * th.part3, th.part2);
  75.         }
  76.     }
  77.  
  78.     public interface Transformer<T extends Comparable<T>> {
  79.  
  80.         String identifier();
  81.         Collection<T> sort(Collection<T> data);
  82.         void iterate(Collection<T> items);
  83.         long getAvgCreation();
  84.         long getAvgIteration();
  85.         long getRuns();
  86.        
  87.     }
  88.  
  89.     public abstract static class TransformerBase<T extends Comparable<T>> implements Transformer<T> {
  90.         protected long creation = 0;
  91.         protected long iteration = 0;
  92.         protected long runs = 0;
  93.  
  94.         @Override
  95.         public long getAvgCreation() {
  96.             return creation / runs;
  97.        
  98.         }
  99.        
  100.         @Override
  101.         public long getAvgIteration() {
  102.             return iteration / runs;
  103.         }
  104.        
  105.         @Override
  106.         public long getRuns() {
  107.             return runs;
  108.         }
  109.        
  110.         @Override
  111.         public void iterate(Collection<T> items) {
  112.             final long before = System.nanoTime();
  113.             for(final T item : items){
  114.             }
  115.             final long after = System.nanoTime();
  116.             iteration += after - before;
  117.             runs++;
  118.         }
  119.        
  120.     }
  121.    
  122.     public static class ArrayListTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
  123.        
  124.         @Override
  125.         public Collection<T> sort(final Collection<T> data){
  126.             final long before = System.nanoTime();
  127.             final List<T> list = new ArrayList<T>(data);
  128.             Collections.sort(list);
  129.             final long after = System.nanoTime();
  130.             creation += after - before;
  131.             return list;
  132.         }
  133.  
  134.         @Override
  135.         public String identifier(){
  136.             return "ArrayListTransformer";
  137.         }
  138.     }
  139.  
  140.     public static class TreeSetTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
  141.  
  142.         @Override
  143.         public Collection<T> sort(final Collection<T> data){
  144.             final long before = System.nanoTime();
  145.             final TreeSet<T> treeSet = new TreeSet<T>(data);
  146.             final long after = System.nanoTime();
  147.             creation += after - before;
  148.             return treeSet;
  149.         }
  150.  
  151.         @Override
  152.         public String identifier(){
  153.             return "TreeSetTransformer";
  154.         }
  155.     }
  156.  
  157.     public static class BestOfBothWorldsTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
  158.  
  159.         @Override
  160.         public Collection<T> sort(final Collection<T> data){
  161.             final long before = System.nanoTime();
  162.             final List<T> list = new ArrayList<T>(new TreeSet<T>(data));
  163.             final long after = System.nanoTime();
  164.             creation += after - before;
  165.             return list;
  166.         }
  167.  
  168.         @Override
  169.         public String identifier(){
  170.             return "BestOfBothWorldsTransformer";
  171.         }
  172.     }
  173.  
  174.    
  175.     public static class PriorityQueueTransformer<T extends Comparable<T>>  extends TransformerBase<T> implements Transformer<T>{
  176.    
  177.         @Override
  178.         public Collection<T> sort(final Collection<T> data){
  179.             final long before = System.nanoTime();
  180.             final PriorityQueue<T> priorityQueue = new PriorityQueue<T>(data);
  181.             final long after = System.nanoTime();
  182.             creation += after - before;
  183.             return priorityQueue;
  184.         }
  185.    
  186.         @Override
  187.         public String identifier(){
  188.             return "PriorityQueueTransformer";
  189.         }
  190.     }
  191.  
  192.  
  193.    
  194.  
  195.     public static void main(final String[] args){
  196.        
  197.         final Random rnd = new Random();
  198.         final int testSize = 4000;
  199.         final int collectionSize = 1000;
  200.  
  201.         Transformer<Thingy> arrayListTransformer = new ArrayListTransformer<Thingy>();
  202.         Transformer<Thingy> treeSetTransformer = new TreeSetTransformer<Thingy>();
  203.         Transformer<Thingy> bestOfBothWorldsTransformer = new BestOfBothWorldsTransformer<Thingy>();
  204.         Transformer<Thingy> priorityQueueTransformer = new PriorityQueueTransformer<Thingy>();
  205.  
  206.         for (int j = 0; j < testSize; j++) {
  207.             Transformer<Thingy> transformer;
  208.             switch (rnd.nextInt(4)) {
  209.                 case 0:  transformer = arrayListTransformer; break;
  210.                 case 1:  transformer = treeSetTransformer; break;
  211.                 case 2:  transformer = bestOfBothWorldsTransformer; break;
  212.                 default: transformer = priorityQueueTransformer; break;
  213.             }
  214.            
  215.             // Note that map & thingies are regenerated each loop, which is more realistic
  216.             final Map<String, Thingy> map = new HashMap<String, Thingy>();
  217.             for(int i = 0; i < collectionSize; i++){
  218.                 final double a = rnd.nextDouble();
  219.                 final double b = rnd.nextDouble();
  220.                 final double c = rnd.nextDouble();
  221.                 map.put("" + a + b + c, new Thingy(a, b, c));
  222.             }
  223.             final Collection<Thingy> thingies = map.values();
  224.            
  225.             final Collection<Thingy> sorted = transformer.sort(thingies);
  226.             transformer.iterate(sorted);
  227.         }
  228.  
  229.         System.out.println("Transformer " + arrayListTransformer.identifier()
  230.                 + " \n\tTotal:      " + format(arrayListTransformer.getAvgCreation() + arrayListTransformer.getAvgIteration())
  231.                 + " \n\tCreation:   " + format(arrayListTransformer.getAvgCreation())
  232.                 + " \n\tIteration:  " + format(arrayListTransformer.getAvgIteration())
  233.                 + " \n\tRun count:  " + arrayListTransformer.getRuns()
  234.                 + " \n\tItem count: " + collectionSize);
  235.  
  236.         System.out.println("Transformer " + treeSetTransformer.identifier()
  237.                 + " \n\tTotal:      " + format(treeSetTransformer.getAvgCreation() + treeSetTransformer.getAvgIteration())
  238.                 + " \n\tCreation:   " + format(treeSetTransformer.getAvgCreation())
  239.                 + " \n\tIteration:  " + format(treeSetTransformer.getAvgIteration())
  240.                 + " \n\tRun count:  " + treeSetTransformer.getRuns()
  241.                 + " \n\tItem count: " + collectionSize);
  242.  
  243.         System.out.println("Transformer " + bestOfBothWorldsTransformer.identifier()
  244.                 + " \n\tTotal:      " + format(bestOfBothWorldsTransformer.getAvgCreation() + bestOfBothWorldsTransformer.getAvgIteration())
  245.                 + " \n\tCreation:   " + format(bestOfBothWorldsTransformer.getAvgCreation())
  246.                 + " \n\tIteration:  " + format(bestOfBothWorldsTransformer.getAvgIteration())
  247.                 + " \n\tRun count:  " + bestOfBothWorldsTransformer.getRuns()
  248.                 + " \n\tItem count: " + collectionSize);
  249.  
  250.         System.out.println("Transformer " + priorityQueueTransformer.identifier()
  251.                 + " \n\tTotal:      " + format(priorityQueueTransformer.getAvgCreation() + priorityQueueTransformer.getAvgIteration())
  252.                 + " \n\tCreation:   " + format(priorityQueueTransformer.getAvgCreation())
  253.                 + " \n\tIteration:  " + format(priorityQueueTransformer.getAvgIteration())
  254.                 + " \n\tRun count:  " + priorityQueueTransformer.getRuns()
  255.                 + " \n\tItem count: " + collectionSize);
  256.  
  257.  
  258.    
  259.     }
  260.  
  261.     private static String format(final long nanoseconds){
  262.         return "" + nanoseconds + " ns (" + (double) nanoseconds / 1000000000
  263.             + " seconds)";
  264.     }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement