Hi Jeff,

Home grown loops are not the best way of micro-benchmarking (have done it myself and learned it). JIT can sometimes optimize the code so aggressively that performance differences you obtain from such benchmarks just show that your concrete program can be optimized better and not that an average program using some function you are trying to measure can run faster too.

I suggest you try to express your benchmark in a tried framework like JMH:

    http://openjdk.java.net/projects/code-tools/jmh/

In particular I suggest studying included samples.

Regards, Peter

On 07/14/2014 02:24 AM, Jeff Hain wrote:

Can you post the benchmark source?
Before the source, here are the time ranges runs were stabilizing in
for lucky executions (using 1.7 for compiler compliance level, and
win7 / core i7) :

          | original | peter7   | peter7   | peter8   | peter8   |
          |          |          | no loop  |          | no loop  |
---------+----------+----------+----------+----------+----------+
jdk7u51  | 0.56-59s | 0.66-68s | 0.62-64s | 0.60-63s | 0.70-74s |
jdk8u20  | 0.54-58s | 0.64-66s | 0.58-60s | 0.58-61s | 0.73-76s |
jdk9     | 0.59-61s | 0.65-67s | 0.73-75s | 0.60-63s | 0.76-78s |

=> The no-loop version seems only better for jdk <= 8 and your webrev 07,
      and for webrev 08, it seems actually (much) worse whatever the jdk.
=> jdk 8 looks faster than 7, but for some reason also faster than 9.


public class IdentityHashMapPerf {
     private static final int MAX_NBR_OF_MAPPINGS = 1*1000;
     private static final int MAX_NBR_OF_CALLS = 100*1000*1000;
     private static final int NBR_OF_RUNS = 10;
public static void main(String[] args) {
         System.out.println(IdentityHashMapPerf.class.getSimpleName()+"...");
         for (int k=0;k<NBR_OF_RUNS;k++) {
             // Quick run for code discovery for k = 0.
             final int maxNbrOfCalls = (k == 0) ? MAX_NBR_OF_MAPPINGS
  : MAX_NBR_OF_CALLS;
             bench_put(new IdentityHashMap<Integer, Integer>(), maxNbrOfCalls);
             //bench_put(new IdentityHashMapPeter7<Integer, Integer>(), 
maxNbrOfCalls);
             //bench_put(new IdentityHashMapPeter7NoLoop<Integer, Integer>(), 
maxNbrOfCalls);
             //bench_put(new IdentityHashMapPeter8<Integer, Integer>(), 
maxNbrOfCalls);
             //bench_put(new IdentityHashMapPeter8NoLoop<Integer, Integer>(), 
maxNbrOfCalls);
         }
         System.out.println("..."+IdentityHashMapPerf.class.getSimpleName());
     }
private static void bench_put(Map<Integer, Integer> map, int maxNbrOfCalls) {
         for (int k=0;k<2;k++) {
             final Integer[] keys = newKeys(MAX_NBR_OF_MAPPINGS);
             final int nbrOfClearAndPuts = maxNbrOfCalls/MAX_NBR_OF_MAPPINGS;
             long a = System.nanoTime();
             {
                 for (int cap=0;cap<nbrOfClearAndPuts;cap++) {
                     map.clear();
                     for (int i=0;i<MAX_NBR_OF_MAPPINGS;i++) {
                         final Integer kv = keys[i];
                         map.put(kv, kv);
                     }
                     if (map.size() != MAX_NBR_OF_MAPPINGS) {
                         throw new AssertionError("anti optim");
                     }
                 }
             }
             long b = System.nanoTime();
             System.out.println(nbrOfClearAndPuts+" * "+MAX_NBR_OF_MAPPINGS
                 +" "+map.getClass().getSimpleName()+".put(,) took "+((b-a)/1e9)+" 
s");
         }
     }
private static Integer[] newKeys(int size) {
         final Integer[] keys = new Integer[size];
         for (int i=0;i<keys.length;i++) {
             keys[i] = i;
         }
         return keys;

     }
}


-Jeff

Reply via email to