1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
// (jEdit options) :folding=explicit:collapseFolds=1:
//{{{ Package, imports
//package quickies;
//import java.awt.*;
//import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.*;
//import java.util.regex.*;
//import javax.swing.*;
//import driftwood.*;
//}}}
/**
* <code>ArrayLookup</code> is intended to test the relative speed
* of several different methods of looking up items in an array.
* <p>The results:
* <ul>
* <li>Direct array access is by far the fastest. It may be up to
* 40 times faster than using a Collection (and at least 6 times).
* <li>All the Collections are within a factor of 3 or so.</li>
* ArrayList is the fastest, and LinkedHashMap is the slowest.
* <li>You pay a 2.3 times penalty (130%) for arbitrary String indices
* as opposed to integer indices.</li>
* Note that in this example, there is strict equality of the
* search key and the found key. This speeds up String.equals().
* <li>You pay a 2.9 times penalty (190%) for using LinkedHashMap
* instead of ArrayList. You pay a 25% penalty for adding the
* linked list capability to a basic HashMap.</li>
* <li>For painting 100,000 points, using a HashMap to lookup color
* would contribute about 10 msec to the render time.</li>
* <li>Tests done with Sun's Java 1.4.2-beta.</li>
* </ul>
* <p>
<pre>
WITH 10x LOOP UNROLLING
======================
For class [Ljava.lang.String;, 83886080 accesses took 79 ms.
For class java.util.ArrayList, 83886080 accesses took 3355 ms.
For class java.util.HashMap, 83886080 accesses took 7881 ms.
For class java.util.LinkedHashMap, 83886080 accesses took 9666 ms.
For class [Ljava.lang.String;, 83886080 accesses took 79 ms.
For class java.util.ArrayList, 83886080 accesses took 3367 ms.
For class java.util.HashMap, 83886080 accesses took 7867 ms.
For class java.util.LinkedHashMap, 83886080 accesses took 9648 ms.
For class [Ljava.lang.String;, 83886080 accesses took 81 ms.
For class java.util.ArrayList, 83886080 accesses took 3358 ms.
For class java.util.HashMap, 83886080 accesses took 7875 ms.
For class java.util.LinkedHashMap, 83886080 accesses took 9642 ms.
WITHOUT LOOP UNROLLING
======================
For class [Ljava.lang.String;, 67108864 accesses took 509 ms.
For class java.util.ArrayList, 67108864 accesses took 3026 ms.
For class java.util.HashMap, 67108864 accesses took 6817 ms.
For class java.util.LinkedHashMap, 67108864 accesses took 8285 ms.
For class [Ljava.lang.String;, 67108864 accesses took 495 ms.
For class java.util.ArrayList, 67108864 accesses took 3024 ms.
For class java.util.HashMap, 67108864 accesses took 6827 ms.
For class java.util.LinkedHashMap, 67108864 accesses took 8286 ms.
For class [Ljava.lang.String;, 67108864 accesses took 495 ms.
For class java.util.ArrayList, 67108864 accesses took 3009 ms.
For class java.util.HashMap, 67108864 accesses took 6796 ms.
For class java.util.LinkedHashMap, 67108864 accesses took 8214 ms.
For class [Ljava.lang.String;, 67108864 accesses took 501 ms.
For class java.util.ArrayList, 67108864 accesses took 2970 ms.
For class java.util.HashMap, 67108864 accesses took 6838 ms.
For class java.util.LinkedHashMap, 67108864 accesses took 8236 ms.
</pre>
*
* <p>Copyright (C) 2003 by Ian W. Davis. All rights reserved.
* <br>Begun on Wed Jun 25 09:48:19 EDT 2003
*/
public class ArrayLookup //extends ... implements ...
{
//{{{ Constants
//}}}
//{{{ Variable definitions
//##################################################################################################
//}}}
//{{{ Constructor(s)
//##################################################################################################
/**
* Constructor
*/
public ArrayLookup()
{
super();
}
//}}}
//{{{ empty_code_segment
//##################################################################################################
//}}}
//{{{ Main, main
//##################################################################################################
/**
* Main() function for running as an application
*/
public void Main()
{
int i;
long time;
Object o;
int index = 2;
String key = "green";
final int reps = 1 << 23;
String[] array = new String[] { "red", "yellow", "green", "blue", "turquoise", "indigo", "cerulean", "aqua", "cornflower", "navy", "azure" };
List list = new ArrayList();
Map map = new HashMap();
Map lmap = new LinkedHashMap();
for(i = 0; i < array.length; i++)
{
list.add(array[i]);
map.put(array[i], array[i]);
lmap.put(array[i], array[i]);
}
System.out.println("Starting access tests with "+(reps*10)+" access attempts.");
// Loops have been unrolled to de-emphasize overhead from looping.
time = System.currentTimeMillis();
for(i = 0; i < reps; i++)
{
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
o = array[index];
}
time = System.currentTimeMillis() - time;
System.out.println("For "+array.getClass()+", "+(reps*10)+" accesses took "+time+" ms.");
time = System.currentTimeMillis();
for(i = 0; i < reps; i++)
{
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
o = list.get(index);
}
time = System.currentTimeMillis() - time;
System.out.println("For "+list.getClass()+", "+(reps*10)+" accesses took "+time+" ms.");
time = System.currentTimeMillis();
for(i = 0; i < reps; i++)
{
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
o = map.get(key);
}
time = System.currentTimeMillis() - time;
System.out.println("For "+map.getClass()+", "+(reps*10)+" accesses took "+time+" ms.");
time = System.currentTimeMillis();
for(i = 0; i < reps; i++)
{
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
o = lmap.get(key);
}
time = System.currentTimeMillis() - time;
System.out.println("For "+lmap.getClass()+", "+(reps*10)+" accesses took "+time+" ms.");
}
public static void main(String[] args)
{
ArrayLookup mainprog = new ArrayLookup();
try
{
mainprog.parseArguments(args);
mainprog.Main();
}
catch(IllegalArgumentException ex)
{
ex.printStackTrace();
System.err.println();
mainprog.showHelp(true);
System.err.println();
System.err.println("*** Error parsing arguments: "+ex.getMessage());
System.exit(1);
}
}
//}}}
//{{{ parseArguments, showHelp
//##################################################################################################
/**
* Parse the command-line options for this program.
* @param args the command-line options, as received by main()
* @throws IllegalArgumentException if any argument is unrecognized, ambiguous, missing
* a required parameter, has a malformed parameter, or is otherwise unacceptable.
*/
void parseArguments(String[] args)
{
String arg, flag, param;
boolean interpFlags = true;
for(int i = 0; i < args.length; i++)
{
arg = args[i];
if(!arg.startsWith("-") || !interpFlags || arg.equals("-"))
{
// This is probably a filename or something
interpretArg(arg);
}
else if(arg.equals("--"))
{
// Stop treating things as flags once we find --
interpFlags = false;
}
else
{
// This is a flag. It may have a param after the = sign
int eq = arg.indexOf('=');
if(eq != -1)
{
flag = arg.substring(0, eq);
param = arg.substring(eq+1);
}
else
{
flag = arg;
param = null;
}
try { interpretFlag(flag, param); }
catch(NullPointerException ex)
{ throw new IllegalArgumentException("'"+arg+"' expects to be followed by a parameter"); }
}
}//for(each arg in args)
}
// Display help information
void showHelp(boolean showAll)
{
if(showAll)
{
InputStream is = getClass().getResourceAsStream("ArrayLookup.help");
if(is == null) System.err.println("\n*** Unable to locate help information in 'ArrayLookup.help' ***\n");
else
{
try { streamcopy(is, System.out); }
catch(IOException ex) { ex.printStackTrace(); }
}
}
System.err.println("quickies.ArrayLookup");
System.err.println("Copyright (C) 2003 by Ian W. Davis. All rights reserved.");
}
// Copies src to dst until we hit EOF
void streamcopy(InputStream src, OutputStream dst) throws IOException
{
byte[] buffer = new byte[2048];
int len;
while((len = src.read(buffer)) != -1) dst.write(buffer, 0, len);
}
//}}}
//{{{ interpretArg, interpretFlag
//##################################################################################################
void interpretArg(String arg)
{
// Handle files, etc. here
}
void interpretFlag(String flag, String param)
{
if(flag.equals("-help") || flag.equals("-h"))
{
showHelp(true);
System.exit(0);
}
else if(flag.equals("-dummy_option"))
{
// handle option here
}
else throw new IllegalArgumentException("'"+flag+"' is not recognized as a valid flag");
}
//}}}
}//class
|