/mobile Handheld Friendly website

 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.530.5317,5161171  0% 0% 0% 100%
600,0001.681.6836,7921171  1% 1% 1% 100%
6,000,00012.6812.69147,9241171  0% 1% 1% 100%

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) 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 08:34:25 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
5.55s 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
3970864 zero
4016489 zero
4012647 zero
one two zero zero zero zero zero zero

 blue red yellow red yellow blue red yellow red blue
1196184 zero
1201656 zero
1202126 zero
1205623 zero
1209274 zero
1198950 zero
1205594 zero
1208470 zero
1182247 zero
1189876 zero
one two zero zero zero zero zero zero

Revised BSD license

  Home   Conclusions   License   Play