Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* see http://stackoverflow.com/questions/3607593/is-it-faster-to-add-to-a-collection-then-sort-it-or-add-to-a-sorted-collection */
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.PriorityQueue;
- import java.util.Random;
- import java.util.TreeSet;
- import org.apache.commons.lang.builder.CompareToBuilder;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- public class TestSort4 {
- public static class Thingy implements Comparable<Thingy>{
- public Thingy(final double part1, final double part2, final double part3){
- this.part1 = part1;
- this.part2 = part2;
- this.part3 = part3;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode(){
- return new HashCodeBuilder() //
- .append(this.part1)
- .append(this.part2)
- .append(this.part3)
- .toHashCode();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(final Object obj){
- boolean result;
- if(obj instanceof Thingy){
- final Thingy other = (Thingy) obj;
- result = new EqualsBuilder() //
- .append(this.part1, other.part1)
- .append(this.part2, other.part2)
- .append(this.part3, other.part3)
- .isEquals();
- } else{
- result = false;
- }
- return result;
- }
- private final double part1;
- private final double part2;
- private final double part3;
- @Override
- public int compareTo(final Thingy other){
- return
- new CompareToBuilder().append(calcComp(this), calcComp(other))
- .toComparison();
- }
- private static double calcComp(final Thingy th){
- return Math.pow(th.part1 * th.part3, th.part2);
- }
- }
- public interface Transformer<T extends Comparable<T>> {
- String identifier();
- Collection<T> sort(Collection<T> data);
- void iterate(Collection<T> items);
- long getAvgCreation();
- long getAvgIteration();
- long getRuns();
- }
- public abstract static class TransformerBase<T extends Comparable<T>> implements Transformer<T> {
- protected long creation = 0;
- protected long iteration = 0;
- protected long runs = 0;
- @Override
- public long getAvgCreation() {
- return creation / runs;
- }
- @Override
- public long getAvgIteration() {
- return iteration / runs;
- }
- @Override
- public long getRuns() {
- return runs;
- }
- @Override
- public void iterate(Collection<T> items) {
- final long before = System.nanoTime();
- for(final T item : items){
- }
- final long after = System.nanoTime();
- iteration += after - before;
- runs++;
- }
- }
- public static class ArrayListTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
- @Override
- public Collection<T> sort(final Collection<T> data){
- final long before = System.nanoTime();
- final List<T> list = new ArrayList<T>(data);
- Collections.sort(list);
- final long after = System.nanoTime();
- creation += after - before;
- return list;
- }
- @Override
- public String identifier(){
- return "ArrayListTransformer";
- }
- }
- public static class TreeSetTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
- @Override
- public Collection<T> sort(final Collection<T> data){
- final long before = System.nanoTime();
- final TreeSet<T> treeSet = new TreeSet<T>(data);
- final long after = System.nanoTime();
- creation += after - before;
- return treeSet;
- }
- @Override
- public String identifier(){
- return "TreeSetTransformer";
- }
- }
- public static class BestOfBothWorldsTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
- @Override
- public Collection<T> sort(final Collection<T> data){
- final long before = System.nanoTime();
- final List<T> list = new ArrayList<T>(new TreeSet<T>(data));
- final long after = System.nanoTime();
- creation += after - before;
- return list;
- }
- @Override
- public String identifier(){
- return "BestOfBothWorldsTransformer";
- }
- }
- public static class PriorityQueueTransformer<T extends Comparable<T>> extends TransformerBase<T> implements Transformer<T>{
- @Override
- public Collection<T> sort(final Collection<T> data){
- final long before = System.nanoTime();
- final PriorityQueue<T> priorityQueue = new PriorityQueue<T>(data);
- final long after = System.nanoTime();
- creation += after - before;
- return priorityQueue;
- }
- @Override
- public String identifier(){
- return "PriorityQueueTransformer";
- }
- }
- public static void main(final String[] args){
- final Random rnd = new Random();
- final int testSize = 4000;
- final int collectionSize = 1000;
- Transformer<Thingy> arrayListTransformer = new ArrayListTransformer<Thingy>();
- Transformer<Thingy> treeSetTransformer = new TreeSetTransformer<Thingy>();
- Transformer<Thingy> bestOfBothWorldsTransformer = new BestOfBothWorldsTransformer<Thingy>();
- Transformer<Thingy> priorityQueueTransformer = new PriorityQueueTransformer<Thingy>();
- for (int j = 0; j < testSize; j++) {
- Transformer<Thingy> transformer;
- switch (rnd.nextInt(4)) {
- case 0: transformer = arrayListTransformer; break;
- case 1: transformer = treeSetTransformer; break;
- case 2: transformer = bestOfBothWorldsTransformer; break;
- default: transformer = priorityQueueTransformer; break;
- }
- // Note that map & thingies are regenerated each loop, which is more realistic
- final Map<String, Thingy> map = new HashMap<String, Thingy>();
- for(int i = 0; i < collectionSize; i++){
- final double a = rnd.nextDouble();
- final double b = rnd.nextDouble();
- final double c = rnd.nextDouble();
- map.put("" + a + b + c, new Thingy(a, b, c));
- }
- final Collection<Thingy> thingies = map.values();
- final Collection<Thingy> sorted = transformer.sort(thingies);
- transformer.iterate(sorted);
- }
- System.out.println("Transformer " + arrayListTransformer.identifier()
- + " \n\tTotal: " + format(arrayListTransformer.getAvgCreation() + arrayListTransformer.getAvgIteration())
- + " \n\tCreation: " + format(arrayListTransformer.getAvgCreation())
- + " \n\tIteration: " + format(arrayListTransformer.getAvgIteration())
- + " \n\tRun count: " + arrayListTransformer.getRuns()
- + " \n\tItem count: " + collectionSize);
- System.out.println("Transformer " + treeSetTransformer.identifier()
- + " \n\tTotal: " + format(treeSetTransformer.getAvgCreation() + treeSetTransformer.getAvgIteration())
- + " \n\tCreation: " + format(treeSetTransformer.getAvgCreation())
- + " \n\tIteration: " + format(treeSetTransformer.getAvgIteration())
- + " \n\tRun count: " + treeSetTransformer.getRuns()
- + " \n\tItem count: " + collectionSize);
- System.out.println("Transformer " + bestOfBothWorldsTransformer.identifier()
- + " \n\tTotal: " + format(bestOfBothWorldsTransformer.getAvgCreation() + bestOfBothWorldsTransformer.getAvgIteration())
- + " \n\tCreation: " + format(bestOfBothWorldsTransformer.getAvgCreation())
- + " \n\tIteration: " + format(bestOfBothWorldsTransformer.getAvgIteration())
- + " \n\tRun count: " + bestOfBothWorldsTransformer.getRuns()
- + " \n\tItem count: " + collectionSize);
- System.out.println("Transformer " + priorityQueueTransformer.identifier()
- + " \n\tTotal: " + format(priorityQueueTransformer.getAvgCreation() + priorityQueueTransformer.getAvgIteration())
- + " \n\tCreation: " + format(priorityQueueTransformer.getAvgCreation())
- + " \n\tIteration: " + format(priorityQueueTransformer.getAvgIteration())
- + " \n\tRun count: " + priorityQueueTransformer.getRuns()
- + " \n\tItem count: " + collectionSize);
- }
- private static String format(final long nanoseconds){
- return "" + nanoseconds + " ns (" + (double) nanoseconds / 1000000000
- + " seconds)";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement