Skip to content

Commit

Permalink
Rewrote PolymorphismTest example from Animal to Coin so that literals…
Browse files Browse the repository at this point in the history
… can be more easily spotted in the assembly.
  • Loading branch information
chriswhocodes committed Jan 31, 2015
1 parent a4515f7 commit e498cf6
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions src/main/resources/examples/PolymorphismTest.java
@@ -1,58 +1,42 @@
public class PolymorphismTest
{
public interface Animal
{
void speak();
}
public interface Coin { void deposit(); }

public static int woofs = 0, miaows = 0, moos = 0;
public static int moneyBox = 0;

public class Dog implements Animal
{
public void speak() { woofs++; }
}
public class Nickel implements Coin { public void deposit() { moneyBox += 5; } }

public class Cat implements Animal
{
public void speak() { miaows++; }
}
public class Dime implements Coin { public void deposit() { moneyBox += 10; } }

public class Cow implements Animal
{
public void speak() { moos++; }
}
public class Quarter implements Coin { public void deposit() { moneyBox += 25; } }

public PolymorphismTest()
{
Animal dog = new Dog();
Animal cat = new Cat();
Animal cow = new Cow();
public PolymorphismTest() {
Coin nickel = new Nickel();
Coin dime = new Dime();
Coin quarter = new Quarter();

Animal creature = null;
Coin coin = null;

// change the variable maxImplementations to control the inlining behaviour
// 2 = bimorphic dispatch - the method call will be inlined
// 3 = polymorphic dispatch - the method call will not be inlined
// 3 = megamorphic dispatch - the method call will not be inlined

final int maxImplementations = 2;

for (int i = 0; i < 100000; i++)
{
switch(i % maxImplementations)
{
case 0: creature = dog; break;
case 1: creature = cat; break;
case 2: creature = cow; break;
for (int i = 0; i < 100000; i++) {
switch(i % maxImplementations) {
case 0: coin = nickel; break;
case 1: coin = dime; break;
case 2: coin = quarter; break;
}

creature.speak();
coin.deposit();
}

System.out.println("Woofs:" + woofs + " Miaows:" + miaows + " Moos:" + moos);
System.out.println("moneyBox:" + moneyBox);
}

public static void main(String[] args)
{
public static void main(String[] args) {
new PolymorphismTest();
}
}

0 comments on commit e498cf6

Please sign in to comment.