Skip to content

Instantly share code, notes, and snippets.

@jamie-allen
Created April 9, 2012 05:44
Show Gist options
  • Save jamie-allen/2341725 to your computer and use it in GitHub Desktop.
Save jamie-allen/2341725 to your computer and use it in GitHub Desktop.
Simple test of the pattern matching by Types in Scala
import scala.annotation.switch
/**
* Simple test of how quickly Scala can perform typed pattern
* matches, both of primitive types and complex object types.
*/
object PatternMatchTest extends App {
val numberOfOperations = 10000000
println("Performing baseline test...")
var startTime = System.currentTimeMillis()
doBaselineTest(new BaselinePatternMatchTester)
println("Total baseline test execution time in milliseconds: " + (System.currentTimeMillis() - startTime))
println("\nPerforming primitive test...")
startTime = System.currentTimeMillis()
doPrimitiveTest(new PrimitivePatternMatchTester)
println("Total primitive test execution time in milliseconds: " + (System.currentTimeMillis() - startTime))
println("\nPerforming complex test...")
startTime = System.currentTimeMillis()
doComplexTest(new ComplexPatternMatchTester)
println("Total complex test execution time in milliseconds: " + (System.currentTimeMillis() - startTime))
def doBaselineTest(patternMatcher: BaselinePatternMatchTester) {
var count = 0
while (count < numberOfOperations) {
(count % 4) match {
case 0 => patternMatcher.patternMatchChar('1')
case 1 => patternMatcher.patternMatchChar('\n')
case 2 => patternMatcher.patternMatchChar('A')
case 3 => patternMatcher.patternMatchChar('$')
}
count += 1
}
println("Performed " + count + " primitive operations out of an expected " + numberOfOperations)
patternMatcher.getMetrics
}
def doPrimitiveTest(patternMatcher: PatternMatcher) {
var count = 0
while (count < numberOfOperations) {
(count % 4) match {
case 0 => patternMatcher.patternMatch(1)
case 1 => patternMatcher.patternMatch(1L)
case 2 => patternMatcher.patternMatch(1F)
case 3 => patternMatcher.patternMatch(1.0)
}
count += 1
}
println("Performed " + count + " primitive operations out of an expected " + numberOfOperations)
patternMatcher.getMetrics
}
def doComplexTest(patternMatcher: PatternMatcher) {
var count = 0
println("Starting test...")
while (count < numberOfOperations) {
(count % 5) match {
case 0 => patternMatcher.patternMatch(A(1))
case 1 => patternMatcher.patternMatch(B(1L))
case 2 => patternMatcher.patternMatch(C(1F))
case 3 => patternMatcher.patternMatch(D(1.0))
case 4 => patternMatcher.patternMatch(E("One"))
}
count += 1
}
println("Performed " + count + " operations out of an expected " + numberOfOperations)
patternMatcher.getMetrics
}
trait PatternMatcher {
var totalTime = 0L
def patternMatch(matchVal: Any)
def getMetrics = {
println("Total time elapsed in test in nanoseconds: " + totalTime)
println("Time per operation in nanoseconds: " + totalTime / numberOfOperations)
}
}
class BaselinePatternMatchTester extends PatternMatcher {
def patternMatch(matchVal: Any) = { }
def patternMatchChar(matchVal: Char) = {
val startTime = System.nanoTime
(matchVal: @switch) match {
case ' ' | '\t' | '\n' => 1
case 'A' | 'Z' | '$' => 2
case '5' => 3
case _ => 4
}
totalTime += (System.nanoTime - startTime)
}
}
class PrimitivePatternMatchTester extends PatternMatcher {
def patternMatch(matchVal: Any) = {
val startTime = System.nanoTime
matchVal match {
case x: Int => x
case x: Long => x
case x: Double => x
case x: Float => x
case _ =>
}
totalTime += (System.nanoTime - startTime)
}
}
sealed trait Sealant
case class A(a: Int) extends Sealant
case class B(b: Long) extends Sealant
case class C(c: Float) extends Sealant
case class D(d: Double) extends Sealant
case class E(e: String) extends Sealant
class ComplexPatternMatchTester extends PatternMatcher {
def patternMatch(matchVal: Any) = {
val startTime = System.nanoTime()
matchVal match {
case x: A => x
case x: B => x
case x: C => x
case x: D => x
case x: E => x
case x: String => x
case _ =>
}
totalTime += (System.nanoTime() - startTime)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment