All pastes #979443 Raw Edit

Unnamed

public java v1 · immutable
·published 2008-04-10 11:25 UTC
rendered paste body
// $Id: hash2.java,v 1.3 2001/05/31 20:37:44 doug Exp $// http://www.bagley.org/~doug/shootout///** This copy courtesy http://www.kano.net/javabench///**  Optimize by 'yanking' common hash2.get() out of both if() and assignment//**  Added Timer() codeimport java.util.*;class Val {    int val;    Val(int init) { val = init; }}public class hash2 {    public static void main(String args[]) {	int n = Integer.parseInt(args[0]);    long start = System.currentTimeMillis();	HashMap<String, Val> hash1 = new HashMap<String, Val>(10000);	HashMap<String, Val> hash2 = new HashMap<String, Val>(n);	for(int i = 0; i < 10000; i++)	    hash1.put("foo_" + Integer.toString(i, 10), new Val(i));	for(int i = 0; i < n; i++) {	    Iterator it = hash1.entrySet().iterator();	    while(it.hasNext()) {			Map.Entry h1 = (Map.Entry)it.next();			String key = (String)h1.getKey();			int v1 = ((Val)h1.getValue()).val;            Object h2 = hash2.get(key); //**			if (h2 != null)				((Val)h2).val += v1;			else				hash2.put(key, new Val(v1));	    }	}    	System.out.print(((Val)hash1.get("foo_1")).val    + " " +	                 ((Val)hash1.get("foo_9999")).val + " " +	                 ((Val)hash2.get("foo_1")).val    + " " +	                 ((Val)hash2.get("foo_9999")).val + "\n");	long end = System.currentTimeMillis();                     System.out.println("Time: " + (end - start) + " ms");       }}