[Assorted-commits] SF.net SVN: assorted:[913] sandbox/trunk/src/java/ReflectionBench.java
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-07-30 03:15:48
|
Revision: 913 http://assorted.svn.sourceforge.net/assorted/?rev=913&view=rev Author: yangzhang Date: 2008-07-30 03:15:58 +0000 (Wed, 30 Jul 2008) Log Message: ----------- added reflection benchmark Added Paths: ----------- sandbox/trunk/src/java/ReflectionBench.java Added: sandbox/trunk/src/java/ReflectionBench.java =================================================================== --- sandbox/trunk/src/java/ReflectionBench.java (rev 0) +++ sandbox/trunk/src/java/ReflectionBench.java 2008-07-30 03:15:58 UTC (rev 913) @@ -0,0 +1,31 @@ +public class ReflectionBench { + public static void main(String[] args) throws Exception { + Object object = new Object(); + Class<?> c = Object.class; + + int loops = 100000; + + long start = System.currentTimeMillis(); + for( int i = 0; i < loops; i++ ) + { + object.toString(); + } + System.out.println( loops + " regular method calls:" + (System.currentTimeMillis() - start) + " milliseconds." ); + java.lang.reflect.Method method = c.getMethod( "toString" ); + + start = System.currentTimeMillis(); + for( int i = 0; i < loops; i++ ) + { + method.invoke( object ); + } + + System.out.println( loops + " reflective method calls without lookup:" + (System.currentTimeMillis() - start) + " milliseconds." ); + start = System.currentTimeMillis(); + for( int i = 0; i < loops; i++ ) + { + method = c.getMethod( "toString" ); + method.invoke( object ); + } + System.out.println( loops + " reflective method calls with lookup:" + (System.currentTimeMillis() - start) + " milliseconds." ); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |