Skip to content

Instantly share code, notes, and snippets.

@takei-shg
Created November 1, 2014 09:54
Show Gist options
  • Save takei-shg/4f91eefdcbfde4d5f0e5 to your computer and use it in GitHub Desktop.
Save takei-shg/4f91eefdcbfde4d5f0e5 to your computer and use it in GitHub Desktop.
testing pattern_match
import java.util.concurrent.atomic.AtomicLong
object Main extends App with LabHelper {
private[this] val random = new scala.util.Random()
private[this] val nameList: Seq[String] = Seq("bob", "bab", "bib")
val resultList = scala.collection.mutable.ArrayBuffer.empty[Long]
for (i <- 1 to 5) {
// average 2111
// resultList += loop(100000000, testCreateSimpleClass)
// average 2133
// resultList += loop(100000000, testCreateMutableClass)
// average = 2056
// resultList += loop(100000000, testCreateCaseClass)
// average 2413
// resultList += loop(100000000, testPersonIf)
// average 2420
// resultList += loop(100000000, testPersonCaseGuard)
// average 2411
// resultList += loop(100000000, testPersonInstanceOfAndIf)
// average 2396
// resultList += loop(100000000, testMutablePersonCaseGuard)
// average 2430
// resultList += loop(100000000, testMutablePersonInstanceOfAndIf)
// average 2418
// resultList += loop(100000000, testCasePersonCase)
// average 2394
resultList += loop(100000000, testCasePersonInstanceOfAndIf)
}
println(resultList.mkString(","))
val listToSum = resultList.drop(1)
println(listToSum.mkString(","))
val sum = listToSum.foldRight(0L)((x,y) => x + y)
println(s"average: ${sum/listToSum.size}")
def testCreateSimpleClass(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
val bob = new Person(nameList(random.nextInt(3)), 12)
counter.getAndIncrement
}
def testCreateMutableClass(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
val mbob = new MutablePerson()
mbob.name = nameList(random.nextInt(3)); mbob.age = 12;
counter.getAndIncrement
}
def testCreateCaseClass(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
val casebob = new CasePerson(nameList(random.nextInt(3)), 12)
counter.getAndIncrement
}
// test person with if
def testPersonIf(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val bob = new Person(nameList(random.nextInt(3)), 12)
if (bob.name != null && bob.name == "bob") {
hitCounter.getAndIncrement
}
}
// test person with case and guard
def testPersonCaseGuard(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val bob = new Person(nameList(random.nextInt(3)), 12)
bob match {
case x : Person if x.name == "bob" => hitCounter.getAndIncrement
case _ =>
}
}
// test person with instanceof and if
def testPersonInstanceOfAndIf(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val bob = new Person(nameList(random.nextInt(3)), 12)
if (bob.isInstanceOf[Person]) {
if (bob.name == "bob") {
hitCounter.getAndIncrement
}
}
}
// test mutable person with case and guard
def testMutablePersonCaseGuard(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val mbob = new MutablePerson()
mbob.name = nameList(random.nextInt(3)); mbob.age = 12;
mbob match {
case y : MutablePerson if y.name == "bob" => hitCounter.getAndIncrement
case _ =>
}
}
// test mutable person with instanceof and if
def testMutablePersonInstanceOfAndIf(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val mbob = new MutablePerson()
mbob.name = nameList(random.nextInt(3)); mbob.age = 12;
if (mbob.isInstanceOf[MutablePerson]) {
if (mbob.name == "bob") {
hitCounter.getAndIncrement
}
}
}
// test case person with case
def testCasePersonCase(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val casebob = new CasePerson(nameList(random.nextInt(3)), 12)
casebob match {
case CasePerson("bob", _) => hitCounter.getAndIncrement
case _ =>
}
}
// test case person with instanceof and if
def testCasePersonInstanceOfAndIf(counter: AtomicLong, hitCounter: AtomicLong): Unit = {
counter.getAndIncrement
val casebob = new CasePerson(nameList(random.nextInt(3)), 12)
if (casebob.isInstanceOf[CasePerson]) {
if (casebob.name == "bob") {
hitCounter.getAndIncrement
}
}
}
}
trait LabHelper {
def loop(count: Int, func: (AtomicLong, AtomicLong) => Unit): Long = {
val counter = new AtomicLong()
val hitCounter = new AtomicLong()
val start = System.currentTimeMillis()
for (i <- 1 to count) {
func(counter, hitCounter)
}
val end = System.currentTimeMillis()
println(s"counter = ${counter.get}")
require(count == counter.get)
println(s"elapsed: ${end - start}")
end - start
}
}
class Person (
val name: String,
val age: Int
)
class MutablePerson {
var name: String = _
var age: Int = _
}
case class CasePerson (
val name: String,
val age: Int
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment