<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: benchmark code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 18:35:42 GMT</pubDate>
    <description>DZone Snippets: benchmark code</description>
    <item>
      <title>Microbenchmark for Bridge vs reflection vs direct access</title>
      <link>http://snippets.dzone.com/posts/show/4806</link>
      <description>&lt;code&gt;import java.lang.reflect.Method;&lt;br /&gt;&lt;br /&gt;public class TimingTest {&lt;br /&gt;	public interface IFoo { public void foo(); }&lt;br /&gt;	public interface PubFoo { public void foo(); }&lt;br /&gt;	public static class Impl implements PubFoo { public void foo(){} }&lt;br /&gt;&lt;br /&gt;	static final int COUNT = 10000000;&lt;br /&gt;	public static void main(String[] args) throws Exception {&lt;br /&gt;		Impl impl = new Impl();&lt;br /&gt;		int count;&lt;br /&gt;&lt;br /&gt;		startTiming("Bridge (including BCEL)");&lt;br /&gt;		IFoo ifoo = Bridge.expose(impl, IFoo.class);&lt;br /&gt;		stopTiming();&lt;br /&gt;		startTiming("Bridge (BCEL loaded)");&lt;br /&gt;		Bridge.expose(impl, PubFoo.class);&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		System.out.println(COUNT+" virtual invocations, warm");&lt;br /&gt;&lt;br /&gt;		startTiming("Direct");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			impl.foo();&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		Method m = impl.getClass().getMethod("foo",new Class[0]);&lt;br /&gt;		startTiming("Reflection");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			m.invoke(impl);&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		startTiming("Bridge");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			ifoo.foo();&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;		System.out.println("1 virtual invocation on "+COUNT+" objects");&lt;br /&gt;&lt;br /&gt;		startTiming("Direct");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			impl.foo();&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		startTiming("Reflection");&lt;br /&gt;		Method tmpM = null;&lt;br /&gt;		Class[] no_args = new Class[0];&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++) {&lt;br /&gt;			if(tmpM == null || tmpM.getDeclaringClass() != impl.getClass())&lt;br /&gt;				tmpM = impl.getClass().getMethod("foo",no_args);&lt;br /&gt;			tmpM.invoke(impl);&lt;br /&gt;		}&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		startTiming("Bridge");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++) {&lt;br /&gt;			IFoo temp = Bridge.expose(impl, IFoo.class);&lt;br /&gt;		}&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		&lt;br /&gt;		System.out.println(COUNT+" interface invocations, warm");&lt;br /&gt;		PubFoo pfoo = (PubFoo)impl;&lt;br /&gt;		Method mi = impl.getClass().getInterfaces()[0].getMethod("foo", new Class[]{});&lt;br /&gt;		IFoo iifoo = Bridge.expose(impl, PubFoo.class, IFoo.class);&lt;br /&gt;&lt;br /&gt;		startTiming("Direct");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			pfoo.foo();&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		startTiming("Reflection");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			mi.invoke(impl);&lt;br /&gt;		stopTiming();&lt;br /&gt;&lt;br /&gt;		startTiming("Bridge");&lt;br /&gt;		for(int i=0; i&lt;COUNT; i++)&lt;br /&gt;			iifoo.foo();&lt;br /&gt;		stopTiming();&lt;br /&gt;		&lt;br /&gt;	}&lt;br /&gt;	static long timer;&lt;br /&gt;	static void startTiming(String task) {&lt;br /&gt;		System.out.printf("  * %12s - ", task);&lt;br /&gt;		System.out.flush();&lt;br /&gt;		timer = System.currentTimeMillis();&lt;br /&gt;	}&lt;br /&gt;	static void stopTiming() {&lt;br /&gt;		long time = System.currentTimeMillis() - timer;&lt;br /&gt;		System.out.println(time+" ms");&lt;br /&gt;	}&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Wed, 21 Nov 2007 11:51:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4806</guid>
      <author>tunah (Sam McCall)</author>
    </item>
  </channel>
</rss>
