<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.theeggeadventure.com/wikimedia/index.php?action=history&amp;feed=atom&amp;title=InstanceOf_Performance</id>
	<title>InstanceOf Performance - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.theeggeadventure.com/wikimedia/index.php?action=history&amp;feed=atom&amp;title=InstanceOf_Performance"/>
	<link rel="alternate" type="text/html" href="https://www.theeggeadventure.com/wikimedia/index.php?title=InstanceOf_Performance&amp;action=history"/>
	<updated>2026-05-13T19:04:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0-beta</generator>
	<entry>
		<id>https://www.theeggeadventure.com/wikimedia/index.php?title=InstanceOf_Performance&amp;diff=2283&amp;oldid=prev</id>
		<title>Brianegge: Created page with &#039;The following is a microbenchmark to test the performance of the Java instanceOf operation.  The four dispatch methods tested are:  # InstanceOf operator # Comparing class object…&#039;</title>
		<link rel="alternate" type="text/html" href="https://www.theeggeadventure.com/wikimedia/index.php?title=InstanceOf_Performance&amp;diff=2283&amp;oldid=prev"/>
		<updated>2009-11-02T03:28:33Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;The following is a microbenchmark to test the performance of the Java instanceOf operation.  The four dispatch methods tested are:  # InstanceOf operator # Comparing class object…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The following is a microbenchmark to test the performance of the Java instanceOf operation.  The four dispatch methods tested are:&lt;br /&gt;
&lt;br /&gt;
# InstanceOf operator&lt;br /&gt;
# Comparing class objects using ==&lt;br /&gt;
# Using polymorphism to dispatch&lt;br /&gt;
# Embedding a type id into each class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
package org.egge;&lt;br /&gt;
&lt;br /&gt;
public class InstanceOf {&lt;br /&gt;
&lt;br /&gt;
	private static final int REPS = 1000000000;&lt;br /&gt;
&lt;br /&gt;
	private enum Type {&lt;br /&gt;
		A, B&lt;br /&gt;
	};&lt;br /&gt;
&lt;br /&gt;
	private static abstract class Base {&lt;br /&gt;
		int sum;&lt;br /&gt;
		final Type type;&lt;br /&gt;
&lt;br /&gt;
		public Base(Type type) {&lt;br /&gt;
			this.type = type;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		public abstract void doSomthing();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static class A extends Base {&lt;br /&gt;
		public A() {&lt;br /&gt;
			super(Type.A);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		public void doA() {&lt;br /&gt;
			sum += 2;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		@Override&lt;br /&gt;
		public void doSomthing() {&lt;br /&gt;
			sum += 2;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static class B extends Base {&lt;br /&gt;
		public B() {&lt;br /&gt;
			super(Type.B);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		public void doB() {&lt;br /&gt;
			sum -= 2;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		@Override&lt;br /&gt;
		public void doSomthing() {&lt;br /&gt;
			sum -= 2;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		for (int i = 0; i &amp;lt; 5; i++) {&lt;br /&gt;
			System.out.println(&amp;quot;Rep &amp;quot; + i);&lt;br /&gt;
			for (Type t : Type.values()) {&lt;br /&gt;
				Base base;&lt;br /&gt;
				if (t == Type.A) {&lt;br /&gt;
					base = new A();&lt;br /&gt;
				} else {&lt;br /&gt;
					base = new B();&lt;br /&gt;
				}&lt;br /&gt;
				testInstanceOf(base);&lt;br /&gt;
				testIsClass(base);&lt;br /&gt;
				testOO(base);&lt;br /&gt;
				testId(base);&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static void testInstanceOf(Base base) {&lt;br /&gt;
		long start = System.currentTimeMillis();&lt;br /&gt;
		for (int i = 0; i &amp;lt; REPS; i++) {&lt;br /&gt;
			if (base instanceof A) {&lt;br /&gt;
				((A) base).doA();&lt;br /&gt;
			} else if (base instanceof B) {&lt;br /&gt;
				((B) base).doB();&lt;br /&gt;
			} else {&lt;br /&gt;
				throw new RuntimeException();&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		long diff = System.currentTimeMillis() - start;&lt;br /&gt;
		System.out.println(&amp;quot;InstanceOf &amp;quot; + diff + &amp;quot; &amp;quot; + base.sum);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static void testOO(Base base) {&lt;br /&gt;
		long start = System.currentTimeMillis();&lt;br /&gt;
		for (int i = 0; i &amp;lt; REPS; i++) {&lt;br /&gt;
			base.doSomthing();&lt;br /&gt;
		}&lt;br /&gt;
		long diff = System.currentTimeMillis() - start;&lt;br /&gt;
		System.out.println(&amp;quot;OO &amp;quot; + diff + &amp;quot; &amp;quot; + base.sum);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static void testId(Base base) {&lt;br /&gt;
		long start = System.currentTimeMillis();&lt;br /&gt;
		for (int i = 0; i &amp;lt; REPS; i++) {&lt;br /&gt;
			switch (base.type) {&lt;br /&gt;
			case A:&lt;br /&gt;
				((A) base).doA();&lt;br /&gt;
				break;&lt;br /&gt;
			case B:&lt;br /&gt;
				((B) base).doB();&lt;br /&gt;
				break;&lt;br /&gt;
			default:&lt;br /&gt;
				throw new RuntimeException();&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		long diff = System.currentTimeMillis() - start;&lt;br /&gt;
		System.out.println(&amp;quot;Id &amp;quot; + diff + &amp;quot; &amp;quot; + base.sum);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private static void testIsClass(Base base) {&lt;br /&gt;
		long start = System.currentTimeMillis();&lt;br /&gt;
		for (int i = 0; i &amp;lt; REPS; i++) {&lt;br /&gt;
			if (base.getClass() == A.class) {&lt;br /&gt;
				((A) base).doA();&lt;br /&gt;
			} else if (base.getClass() == B.class) {&lt;br /&gt;
				((B) base).doB();&lt;br /&gt;
			} else {&lt;br /&gt;
				throw new RuntimeException();&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		long diff = System.currentTimeMillis() - start;&lt;br /&gt;
		System.out.println(&amp;quot;class== &amp;quot; + diff + &amp;quot; &amp;quot; + base.sum);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The results are significantly different when running on a Solaris server JVM vs a Windows client JVM.  On the server, the results of the four methods are nearly identical.  The interesting thing with the server is the first iteration runs &amp;#039;&amp;#039;very&amp;#039;&amp;#039; quickly.  This is because only one type has been loaded at that point and the operations can easily be optimized, and there&amp;#039;s only one possible path.  The results on Windows, show the fastest method is comparing class objects, then instanceof, then polymorphism, and lastly the type id.  &lt;br /&gt;
&lt;br /&gt;
== Results ==&lt;br /&gt;
&amp;lt;geshi&amp;gt;&lt;br /&gt;
Windows:&lt;br /&gt;
InstanceOf 6141 &lt;br /&gt;
class== 3312&lt;br /&gt;
OO 8547 &lt;br /&gt;
Id 9953&lt;br /&gt;
&lt;br /&gt;
Solaris first:&lt;br /&gt;
InstanceOf 121 &lt;br /&gt;
class== 122 &lt;br /&gt;
OO 114 &lt;br /&gt;
Id 119 &lt;br /&gt;
&lt;br /&gt;
Solaris last:&lt;br /&gt;
InstanceOf 3156 &lt;br /&gt;
class== 2925 &lt;br /&gt;
OO 3083 &lt;br /&gt;
Id 3067 &lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Java]]&lt;br /&gt;
[[Category:Performance]]&lt;/div&gt;</summary>
		<author><name>Brianegge</name></author>
	</entry>
</feed>