ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/MapMicroBenchmark.java
(Generate patch)

Comparing jsr166/src/test/loops/MapMicroBenchmark.java (file contents):
Revision 1.3 by dl, Sun Jul 26 15:40:47 2009 UTC vs.
Revision 1.17 by jsr166, Thu Jan 15 18:34:19 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 import java.util.*;
7   import java.io.*;
8   import java.math.*;
9 + import java.util.*;
10  
11   /**
12   * A micro-benchmark with key types and operation mixes roughly
13   * corresponding to some real programs.
14 < *
14 > *
15   * The main results are a table of approximate nanoseconds per
16   * element-operation (averaged across get, put etc) for each type,
17   * across a range of map sizes. It also includes category "Mixed"
# Line 24 | Line 24 | import java.math.*;
24   * dynamic type specialization.  Some test classes, like Float and
25   * BigDecimal are included not because they are commonly used as keys,
26   * but because they can be problematic for some map implementations.
27 < *
27 > *
28   * By default, it creates and inserts in order dense numerical keys
29   * and searches for keys in scrambled order. Use "s" as second arg to
30   * instead insert and search in unscrambled order.
31   *
32   * For String keys, the program tries to use file "testwords.txt", which
33   * is best used with real words.  We can't check in this file, but you
34 < * can create one from a real dictonary (1 line per word) and then run
34 > * can create one from a real dictionary (1 line per word) and then run
35   * linux "shuf" to randomize entries. If no file exists, it uses
36   * String.valueOf(i) for element i.
37   */
38   public class MapMicroBenchmark {
39      static final String wordFile = "testwords.txt";
40  
41 <    static Class mapClass;
41 >    static Class<?> mapClass;
42      static boolean randomSearches = true;
43  
44      // Nanoseconds per run
# Line 61 | Line 61 | public class MapMicroBenchmark {
61              System.out.println("Usage: java MapMicroBenchmark className [r|s]keys [r|s]searches");
62              return;
63          }
64 <            
64 >
65          mapClass = Class.forName(args[0]);
66  
67          if (args.length > 1) {
# Line 102 | Line 102 | public class MapMicroBenchmark {
102          jobs[2] = new Job("Integer   ", is, Integer.class);
103  
104          Object[] ls = new Object[n];
105 <        for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long)i);
105 >        for (int i = 0; i < n; i++) ls[i] = Long.valueOf((long) i);
106          jobs[3] = new Job("Long      ", ls, Long.class);
107  
108          Object[] fs = new Object[n];
109 <        for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float)i);
109 >        for (int i = 0; i < n; i++) fs[i] = Float.valueOf((float) i);
110          jobs[4] = new Job("Float     ", fs, Float.class);
111  
112          Object[] ds = new Object[n];
113 <        for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double)i);
113 >        for (int i = 0; i < n; i++) ds[i] = Double.valueOf((double) i);
114          jobs[5] = new Job("Double    ", ds, Double.class);
115  
116          Object[] bs = new Object[n];
# Line 143 | Line 143 | public class MapMicroBenchmark {
143          warmup1(mixed);
144          warmup3(jobs);
145          Thread.sleep(500);
146 <        time(jobs);
146 >        time(jobs);
147      }
148  
149      static void runWork(Job[] jobs, int minIters, int maxIters, long timeLimit) throws Throwable {
150 <        for (int k = 0; k <  nsizes; ++k) {
150 >        for (int k = 0; k < nsizes; ++k) {
151              int len = sizes[k];
152              for (int i = 0; i < jobs.length; i++) {
153                  Thread.sleep(50);
# Line 160 | Line 160 | public class MapMicroBenchmark {
160  
161      // First warmup -- run only mixed job to discourage type specialization
162      static void warmup1(Job job) throws Throwable {
163 <        for (int k = 0; k <  nsizes; ++k)
163 >        for (int k = 0; k < nsizes; ++k)
164              job.work(sizes[k], 1, 1, 0);
165      }
166  
# Line 193 | Line 193 | public class MapMicroBenchmark {
193          long[] aves = new long[nsizes];
194          int njobs = jobs.length;
195  
196 <        for (int i = 0; i < njobs; i++) {
196 >        for (int i = 0; i < njobs; i++) {
197              System.out.print(jobs[i].name);
198              for (int k = 0; k < nsizes; ++k) {
199                  long nanos = jobs[i].nanos[k];
# Line 210 | Line 210 | public class MapMicroBenchmark {
210          System.out.println("\n");
211      }
212  
213
213      static final class Job {
214 <        final String name;
215 <        final Class elementClass;
214 >        final String name;
215 >        final Class<?> elementClass;
216          long[] nanos = new long[nsizes];
217          final Object[] items;
218          Object[] searches;
219          volatile long checkSum;
220          volatile int lastSum;
221 <        Job(String name, Object[] items, Class elementClass) {
221 >        Job(String name, Object[] items, Class<?> elementClass) {
222              this.name = name;
223              this.items = items;
224              this.elementClass = elementClass;
# Line 236 | Line 235 | public class MapMicroBenchmark {
235          public long work(int len, int minIters, int maxIters, long timeLimit) {
236              Map m;
237              try {
238 <                m = (Map)mapClass.newInstance();
239 <            } catch(Exception e) {
238 >                m = (Map) mapClass.newInstance();
239 >            } catch (Exception e) {
240                  throw new RuntimeException("Can't instantiate " + mapClass + ": " + e);
241              }
242              Object[] ins = items;
# Line 261 | Line 260 | public class MapMicroBenchmark {
260                  sum += len - half;
261                  for (int i = 0; i < len; ++i) {
262                      Object x = keys[i];
263 <                    Object v = m.get(x);
263 >                    Object v = m.get(x);
264                      if (elementClass.isInstance(v)) // touch v
265                          ++sum;
266                  }
# Line 273 | Line 272 | public class MapMicroBenchmark {
272                  }
273                  checkSum += sum ^ (sum << 3);
274                  for (Object e : m.keySet()) {
275 <                    if (elementClass.isInstance(e))
275 >                    if (elementClass.isInstance(e))
276                          ++sum;
277                  }
278                  checkSum += sum ^ (sum << 4);
279                  for (Object e : m.values()) {
280 <                    if (elementClass.isInstance(e))
280 >                    if (elementClass.isInstance(e))
281                          ++sum;
282                  }
283                  checkSum += sum ^ (sum << 5);
# Line 334 | Line 333 | public class MapMicroBenchmark {
333                      if (m.put(x, x) == null)
334                          ++sum;
335                  }
336 +                /*  // uncomment to avoid calling clear()
337 +                for (int i = 0; i < len; ++i) {
338 +                    Object x = keys[i];
339 +                    m.remove(x);
340 +                }
341 +                */
342                  m.clear();
343                  sum += len - (quarter * 2);
344                  checkSum += sum ^ (sum << 12);
345  
346                  if (j == 0 && sum != lastSum + len * OPS_PER_ITER)
347                      throw new Error(name);
348 <                
348 >
349                  elapsed = System.nanoTime() - startTime;
350                  ++j;
351                  if (j >= minIters &&
352                      (j >= maxIters || elapsed >= timeLimit))
353                      break;
354                  // non-warmup - swap some keys for next insert
355 <                if (minIters != 1 && randomSearches)
355 >                if (minIters != 1 && randomSearches)
356                      shuffleSome(ins, len, len >>> 3);
357              }
358 <            long ops = ((long)j) * len * OPS_PER_ITER;
358 >            long ops = ((long) j) * len * OPS_PER_ITER;
359              lastSum = sum;
360              return elapsed / ops;
361          }
362  
363      }
364  
360
365      static final Random rng = new Random(3122688);
366  
367      // Shuffle the subarrays for each size. This doesn't fully
# Line 365 | Line 369 | public class MapMicroBenchmark {
369      // more realistic
370      static void scramble(Object[] a) {
371          for (int k = 0; k < sizes.length; ++k) {
372 <            int origin = k == 0? 0 : sizes[k-1];
372 >            int origin = (k == 0) ? 0 : sizes[k-1];
373              for (int i = sizes[k]; i > origin + 1; i--) {
374                  Object t = a[i-1];
375                  int r = rng.nextInt(i - origin) + origin;
# Line 373 | Line 377 | public class MapMicroBenchmark {
377                  a[r] = t;
378              }
379          }
380 <    }            
380 >    }
381  
382      // plain array shuffle
383      static void shuffle(Object[] a, int size) {
384 <        for (int i= size; i>1; i--) {
384 >        for (int i = size; i > 1; i--) {
385              Object t = a[i-1];
386              int r = rng.nextInt(i);
387              a[i-1] = a[r];
# Line 425 | Line 429 | public class MapMicroBenchmark {
429      }
430  
431      // Read in String keys from file if possible
432 <    static void initStringKeys(Object[] keys, int n) throws Exception {
432 >    static void initStringKeys(Object[] keys, int n) throws Exception {
433          FileInputStream fr = null;
434          try {
435              fr = new FileInputStream(wordFile);
# Line 438 | Line 442 | public class MapMicroBenchmark {
442  
443          BufferedInputStream in = new BufferedInputStream(fr);
444          int k = 0;
445 <        outer:while (k < n) {
445 >        outer: while (k < n) {
446              StringBuffer sb = new StringBuffer();
447              for (;;) {
448                  int c = in.read();
449 <                if (c < 0)
449 >                if (c < 0)
450                      break outer;
451 <                char ch = (char)c;
451 >                char ch = (char) c;
452                  if (ch == '\n') {
453                      keys[k++] = sb.toString();
454                      break;
# Line 458 | Line 462 | public class MapMicroBenchmark {
462          // fill up remaining keys with path-like compounds of previous pairs
463          int j = 0;
464          while (k < n)
465 <            keys[k++] = (String)keys[j++] + "/" + (String)keys[j];
465 >            keys[k++] = (String) keys[j++] + "/" + (String) keys[j];
466      }
467  
468   }
465

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines