Could the compiler being doing some optimization?

Version 1: The compiler might be able to infer that a HashMap is being used so it doesn't need a late-binding lookup on Map.

Version 2: The late-binding lookup is needed since a Map is passed into the method.

What happens if test(Map map) changes to test(HashMap map) for your test?

-Tim


[EMAIL PROTECTED] wrote:
from:    "Shapira, Yoav" <[EMAIL PROTECTED]>

Test class #2)
public static void main(String[] args) {
Map map = new HashMap();
map.put("AAA");
map.put("BBB");
map.put("CCC");

test(map);
}
private static void test(Map map) {
long start = 0;
long end = 0;

start = System.currentTimeMillis();
for (int i = RUNS; i >= 0; i--) {
  map.get("AAA");
  map.get("BBB");
  map.get("CCC");
}
end = System.currentTimeMillis();
System.out.println(end - start);
}

These two classes give very different results. The refactored version
#2 is

much slower, between 20% and 80% depending on the type of Map.

Am I missing something obvious???
[Sun JDK 1.4.1_01 (client or server)]
I don't think you're missing something obvious.  But out of curiosity,
if you mark the test(Map) method final, does the performance come out
equal to version #1 of your class?  It should, as the 1.4.1 compiler
will inline the method.

Also, does the 20% difference reduce when you increase RUNS?  If RUNS is
a very small value, then the method invocation overhead can actually be
pretty high when compared to the run itself.  Make RUNS 100000 and
measure the difference between the classes again, if you haven't done so
already...

The value I'm using for RUNS is 10,000,000, giving total time of the order of 2-7 seconds for each loop.

Also, if you look at the code, the measurement (start to end) is all within the test(Map) method and doesn't include method invocations. Thats whats so strange...

Stephen

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to