performance measurements

Each table row shows performance measurements for this Scala program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
60,0000.930.4523,4121171  67% 39% 44% 60%
600,0004.181.5738,2521171  41% 74% 73% 82%
6,000,00036.6512.5546,5921171  69% 80% 79% 67%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read chameneos-redux benchmark to see what this program should do.

 notes

java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

Scala compiler version 2.10.3 -- Copyright 2002-2013, LAMP/EPFL

 chameneos-redux Scala #2 program source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   Contributed by Eric Willigers
   Port of Java implementation by Michael Barker and Luzius Meisser
*/

object Colours extends Enumeration {
   val Blue = Value("blue")
   val Red = Value("red")
   val Yellow = Value("yellow")
}

import Colours.{Blue, Red, Yellow, Value => Colour}

final class Creature(place: MeetingPlace, var colour: Colour) extends Runnable {
   val id = System.identityHashCode(this)
   var sameCount = 0
   var count = 0

   def run() = try {
      while (true) {
         val p = place.meet(id, colour)
         colour = p.colour
         if (p.sameId)
            sameCount += 1
         count +=1
      }
   } catch {
      case _: Exception => ()
   }

   override def toString = String.valueOf(count)+" "+chameneosredux.getNumber(sameCount)
}

final class MeetingPlace(var meetingsLeft: Int) {
   var firstColour: Option[Colour] = None
   var firstId = 0
   var current: Future = _

   def meet(id: Int, c: Colour) = synchronized {
      if (meetingsLeft == 0) {
         throw new Exception("Finished")
      } else {
         if (firstColour.isEmpty) {
            firstColour = Some(c)
            firstId = id
            current = new Future()
         } else {
            current.setItem(new Pair(id == firstId, chameneosredux.doCompliment(c, firstColour.get)))
            firstColour = None
            meetingsLeft -= 1
         }

         current
      }
   }.getItem()
}

final class Future {
   @volatile var p: Pair = _

   def getItem() = {
      while (p == null)
         Thread.`yield`()   
      p
   }

   def setItem(_p: Pair) {
      this.p = _p
   }
}

final case class Pair(sameId: Boolean, colour: Colour)

object chameneosredux {
   def doCompliment(c1: Colour, c2: Colour) = (c1, c2) match {
      case (Blue, Blue) => Blue   
      case (Blue, Red) => Yellow   
      case (Blue, Yellow) => Red   
      case (Red, Blue) => Yellow
      case (Red, Red) => Red
      case (Red, Yellow) => Blue   
      case (Yellow, Blue) => Red   
      case (Yellow, Red) => Blue
      case (Yellow, Yellow) => Yellow
   }

   def run(n: Int, colours: Colour*) {
      val place = new MeetingPlace(n)
      colours.foreach { c => print(" "+c) }
      val creatures = colours.map { new Creature(place, _) }.toArray
      println()
      val ts = creatures.map { new Thread(_) }
      ts.foreach { _.start() }
      ts.foreach { _.join() }
      creatures.foreach { println(_) }
      println(getNumber(creatures.foldLeft(0){_ + _.count}))
      println()
   }

   def main(args: Array[String]) {
      val n = if (args.isEmpty) 600 else Integer.parseInt(args(0))
      printColours()
      println()
      run(n, Blue, Red, Yellow)
      run(n, Blue, Red, Yellow, Red, Yellow,
            Blue, Red, Yellow, Red, Blue)
   }

   val Numbers = Array[String]("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")

   def getNumber(n: Int) = String.valueOf(n).toList.map { ch => Numbers(Character.getNumericValue(ch)) } .mkString(" ")

   def printColours() {
      printColours(Blue, Blue)
      printColours(Blue, Red)
      printColours(Blue, Yellow)
      printColours(Red, Blue)   
      printColours(Red, Red)
      printColours(Red, Yellow)
      printColours(Yellow, Blue)
      printColours(Yellow, Red)
      printColours(Yellow, Yellow)
   }

   def printColours(c1: Colour, c2: Colour) {
      println(c1+" + "+c2+" -> "+doCompliment(c1, c2))   
   }
}

 make, command-line, and program output logs

Wed, 19 Mar 2014 02:10:58 GMT

MAKE:
mv chameneosredux.scala-2.scala chameneosredux.scala
/usr/local/src/scala-2.10.3/bin/scalac -optimise -target:jvm-1.7 chameneosredux.scala
6.28s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0/bin/java -server -XX:+TieredCompilation -XX:+AggressiveOpts  -Xbootclasspath/a:/usr/local/src/scala-2.10.3/lib/scala-library.jar:/usr/local/src/scala-2.10.3/lib/akka-actors.jar:/usr/local/src/scala-2.10.3/lib/typesafe-config.jar chameneosredux 6000000

PROGRAM OUTPUT:
blue + blue -> blue
blue + red -> yellow
blue + yellow -> red
red + blue -> yellow
red + red -> red
red + yellow -> blue
yellow + blue -> red
yellow + red -> blue
yellow + yellow -> yellow

 blue red yellow
4142482 zero
3780704 zero
4076814 zero
one two zero zero zero zero zero zero

 blue red yellow red yellow blue red yellow red blue
1224203 zero
1220119 zero
1046649 zero
1210258 zero
1231478 zero
1199977 zero
1216986 zero
1211858 zero
1215921 zero
1222551 zero
one two zero zero zero zero zero zero

Revised BSD license

  Home   Conclusions   License   Play