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.850.4220,9241171  39% 93% 44% 35%
600,0004.421.6843,8161171  61% 72% 83% 49%
6,000,00037.4313.5758,4641171  71% 65% 75% 69%

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_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)

Scala compiler version 2.11.4 -- 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 Nov 2014 03:31:25 GMT

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

COMMAND LINE:
 /usr/local/src/jdk1.8.0_25/bin/java -server -XX:+TieredCompilation -XX:+AggressiveOpts  -Xbootclasspath/a:/usr/local/src/scala-2.11.4/lib/scala-library.jar:/usr/local/src/scala-2.11.4/lib/akka-actors.jar:/usr/local/src/scala-2.11.4/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
4013584 zero
4448177 zero
3538239 zero
one two zero zero zero zero zero zero

 blue red yellow red yellow blue red yellow red blue
1182935 zero
1212978 zero
1206953 zero
1202600 zero
1214536 zero
1211272 zero
1212910 zero
1220334 zero
1141332 zero
1194150 zero
one two zero zero zero zero zero zero

Revised BSD license

  Home   Conclusions   License   Play