Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Last active January 11, 2016 05:50
Show Gist options
  • Save guyhughes/43ecf0b1d2cc917e176c to your computer and use it in GitHub Desktop.
Save guyhughes/43ecf0b1d2cc917e176c to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
/*
* __ __ __ ____ ______ _ __
* / / / /__ ____ _____/ /____/ __ \____/_ __/___ _(_) /____
* / /_/ / _ \/ __ `/ __ / ___/ / / / ___// / / __ `/ / / ___/
* / __ / __/ /_/ / /_/ (__ ) /_/ / / / / / /_/ / / (__ )
* /_/ /_/\___/\__,_/\__,_/____/\____/_/ /_/ \__,_/_/_/____/
* https://github.com/guyhughes
*/
class HeadsOrTails {
public static final int ITERATIONS = 100000;
public static void main(String[] args){
long start;
double head_time,tail_time;
ArrayList<Integer> a = new ArrayList<>();
System.out.printf("Iterations: %s\n",ITERATIONS);
start = System.currentTimeMillis();
for(int x=0; x < ITERATIONS; x++)
a.add(new Integer(0));
tail_time = (System.currentTimeMillis()-start)/1000.0;
System.out.printf("Adding to tail took %,5.4fs\n",tail_time);
start = System.currentTimeMillis();
a.clear();
for(int x=0; x < ITERATIONS; x++)
a.add(0,new Integer(0));
head_time = (System.currentTimeMillis()-start)/1000.0;
System.out.printf("Adding to head took %,5.4fs\n",head_time);
System.out.printf("Therefore adding to tail was %,5.2fx faster than adding to head for %d iterations.\n",head_time/tail_time,ITERATIONS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment