| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
import java.io.*; |
|---|
| 7 |
import java.util.*; |
|---|
| 8 |
|
|---|
| 9 |
class ComplexityTest { |
|---|
| 10 |
public static void main(String argv[]) { |
|---|
| 11 |
TreeSet t = new TreeSet(); |
|---|
| 12 |
|
|---|
| 13 |
if(argv.length < 4) { |
|---|
| 14 |
System.out.println("Usage - java ComplexityTest min max step trials"); |
|---|
| 15 |
return; |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
int min = Integer.parseInt(argv[0]); |
|---|
| 19 |
int max = Integer.parseInt(argv[1]); |
|---|
| 20 |
int step = Integer.parseInt(argv[2]); |
|---|
| 21 |
int trials = Integer.parseInt(argv[3]); |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
float [][]performance = new float[(max-min)/step+1][4]; |
|---|
| 25 |
|
|---|
| 26 |
for(int n = min, i = 0; n <= max && i < performance.length; n += step, ++i) { |
|---|
| 27 |
|
|---|
| 28 |
performance[i][0]=n; |
|---|
| 29 |
|
|---|
| 30 |
for(int trial = 0; trial < trials; ++trial) { |
|---|
| 31 |
|
|---|
| 32 |
System.out.println("Trial " + trial + " of " + trials); |
|---|
| 33 |
System.out.println("Inserting " + n + " random values."); |
|---|
| 34 |
Random random = new Random(); |
|---|
| 35 |
System.gc(); |
|---|
| 36 |
long time = System.currentTimeMillis(); |
|---|
| 37 |
for(int j = 0; j < n; ++j) t.add(new Integer(random.nextInt())); |
|---|
| 38 |
time = System.currentTimeMillis() - time; |
|---|
| 39 |
performance[i][1] += (double)time / (double)n * 1.0e6; |
|---|
| 40 |
System.out.println("Total time: " + time + " ms" ); |
|---|
| 41 |
System.out.println("Average time: " + performance[i][1] + " ns"); |
|---|
| 42 |
|
|---|
| 43 |
System.out.println("Finding the lowest value... (pinky to lips) ONE MILLION TIMES"); |
|---|
| 44 |
Integer lowest = null; |
|---|
| 45 |
System.gc(); |
|---|
| 46 |
time = System.currentTimeMillis(); |
|---|
| 47 |
for(int j = 0; j < 1000000; ++j) lowest = (Integer)t.first(); |
|---|
| 48 |
time = System.currentTimeMillis() - time; |
|---|
| 49 |
performance[i][2] += time; |
|---|
| 50 |
System.out.println("That took " + time + " ns per operation"); |
|---|
| 51 |
|
|---|
| 52 |
System.out.println("Removing the lowest value, then adding a random one, 1e6 times"); |
|---|
| 53 |
System.gc(); |
|---|
| 54 |
time = System.currentTimeMillis(); |
|---|
| 55 |
for(int j = 0; j < 1000000; ++j) { |
|---|
| 56 |
lowest = (Integer)t.first(); |
|---|
| 57 |
t.remove(lowest); |
|---|
| 58 |
t.add(new Integer(random.nextInt())); |
|---|
| 59 |
} |
|---|
| 60 |
time = System.currentTimeMillis() - time; |
|---|
| 61 |
performance[i][3] += time; |
|---|
| 62 |
System.out.println("That took " + time + " ns per operation"); |
|---|
| 63 |
} |
|---|
| 64 |
for(int j = 1; j <= 3; ++j) |
|---|
| 65 |
performance[i][j] /= trials; |
|---|
| 66 |
} |
|---|
| 67 |
System.out.println("N\tInsert\tLowest\tRemoveLowest"); |
|---|
| 68 |
for(int i = 0; i < performance.length; ++i) { |
|---|
| 69 |
System.out.println( |
|---|
| 70 |
(int)performance[i][0] + "\t" + |
|---|
| 71 |
(int)performance[i][1] + "\t" + |
|---|
| 72 |
performance[i][2] + "\t" + |
|---|
| 73 |
performance[i][3]); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|