Skip to content

Instantly share code, notes, and snippets.

@DRMacIver
Created February 19, 2009 21:55
Show Gist options
  • Select an option

  • Save DRMacIver/67138 to your computer and use it in GitHub Desktop.

Select an option

Save DRMacIver/67138 to your computer and use it in GitHub Desktop.
abstract class Foo(val tag : Int){
}
class Bar() extends Foo(0){
}
class Baz() extends Foo(1){
}
class Bif() extends Foo(2){
}
class Blort() extends Foo(3){
}
object Main{
def size = 100000000;
val items = new Array[Foo](size);
println("Items allocated")
val Bar = new Bar();
val Baz = new Baz();
val Bif = new Bif();
val Blort = new Blort();
var i = 0;
while(i < items.length){
items(i) = (i % 4) match {
case 0 => Bar;
case 1 => Baz;
case 2 => Bif;
case 3 => Blort;
}
i += 1;
}
println("Items filled");
var bar = 0;
var baz = 0;
var bif = 0;
var blort = 0;
def switchAt(i : Int) = items(i).tag match {
case 0 => bar += 1;
case 1 => baz += 1;
case 2 => bif += 1;
case 3 => blort += 1;
}
def instanceAt(i : Int) = {
if (items(i).isInstanceOf[Bar]) bar += 1;
else if (items(i).isInstanceOf[Baz]) baz += 1;
else if (items(i).isInstanceOf[Bif]) bif += 1;
else blort += 1;
}
def doSwitch = {
val start = System.currentTimeMillis;
var i = 0;
while (i < items.length){
switchAt(i);
i += 1;
}
System.currentTimeMillis - start;
}
def doInstance = {
val start = System.currentTimeMillis;
var i = 0;
while (i < items.length){
instanceAt(i);
i += 1;
}
System.currentTimeMillis - start;
}
def main(args : Array[String]){
println("Starting tests...")
for(_ <- 0 until 10){
println("Switch: " + doSwitch);
println("Instance: " + doInstance);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment