|
From: James N. <jam...@ya...> - 2002-01-09 22:07:17
|
finn
interesting benchmark, interesting results. what a performance rift.
the conversation has uncovered a general irrelevance of the coding style
details, there is a huge win in writing the alternative reflection code for
jdk1.4 and keeping instanceof for ibm. (and a huge win in avoiding jdk 1.3)
perhaps a bogo-reflection-mips score could load alternative classes to do
collection class introspections.
experimentation results:
first, your benchmark as-is on my dual celeron 500 w2k:
java version "1.3.1_01" Java(TM) 2 Runtime Environment, Standard Edition
(build 1.3.1_01) Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)
cd c:/tmp/
/opt/jdk/bin/javac x.java;/opt/jdk/bin/java x
instanceof 7500
reflect 12656
instanceof 7812
reflect 12610
instanceof 6984
reflect 11563
instanceof 7015
reflect 11469
2nd, after adding some inner nameless classes, sprinkling a few statics and
finals and removing the new [0]'s, perhaps invalidating the intent of the
benchmarks...
instanceof 7563
reflect x - 11719
instanceof 8844
reflect x$1 - 24234
instanceof 9063
reflect x$2 - 22531
3rd, making "call" final and static (code below).
reflect x - 9500
reflect x$1 - 23844
reflect x$2 - 22031
...looping on " o.getClass().getMethod(intern_methodname,
no_Class).invoke(o,no_Object);"
instanceof 7765
reflect x - 118641
reflect x$1 - 119734
reflect x$2 - 116109
...looping on "call()"
reflect x - 47
reflect x - 47
reflect x - 32
...making call non-static non-final looping on ((x)o).call()
reflect x - ~157
...making call final looping as above
reflect x - ~93.5
---
import java.lang.reflect.*;
public class x {
public final static void main(String args[]) throws Exception {
meassure(new x (), true);
meassure(new Object(){public void call() { cnt++; }},true );
meassure(new Object(){public final void call() { cnt++; }},true );
}
public static final Class[] no_Class=new Class[]{};
public static final Object[] no_Object=new Object[]{};
private final static String intern_methodname="call".intern();
public final static void meassure(Object o, boolean print)
throws Exception
{
long now;
int i;
Class xcls;
Method m;
now = System.currentTimeMillis();
for (i = 0; i < LOOPS; i++) {
if (o instanceof java.util.Vector) { cnt++; }
if (o instanceof java.util.Dictionary) { cnt++; }
//if (o instanceof java.util.Enumeration) { cnt++; }
if (o instanceof java.util.List) { cnt++; }
if (o instanceof java.util.Collection) { cnt++; }
if (o instanceof java.util.Map) { cnt++; }
//if (o instanceof java.util.Iterator) { cnt++; }
if (o instanceof x) { cnt++; }
}
System.out.println("instanceof " +
(System.currentTimeMillis() - now));
xcls = o.getClass();
m = xcls.getMethod(intern_methodname, no_Class);
now = System.currentTimeMillis();
for (i = 0; i < LOOPS; i++) {
m.invoke(o, no_Object);
}
System.out.println("reflect " +o.getClass().getName()+" - "+
(System.currentTimeMillis() - now));
}
public static final int LOOPS = 3000000;
static int cnt = 0;
public static final void call() { cnt++; }
}
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|