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,000Failed  1271   

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 program source code

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

   Scala translation contributed by Rex Kerr;
   based on Java original by Michael Barker.
*/


import java.util.concurrent._;
import atomic._;

object chameneosredux {
  object Color extends Enumeration(0,"blue","red","yellow") {
    val Blue,Red,Yellow = Value
    def doComplement(c1: Value, c2: Value) = c1 match {
      case Blue => c2 match {
        case Blue => Blue
        case Red => Yellow
        case Yellow => Red
      }
      case Red => c2 match {
        case Blue => Yellow
        case Red => Red
        case Yellow => Blue
      }
      case Yellow => c2 match {
        case Blue => Red
        case Red => Blue
        case Yellow => Yellow
      }
    }
    def printColors {
      Color.foreach(c1 => Color.foreach(c2 => {
        println(c1 + " + " + c2 + " -> " + doComplement(c1,c2))
      }))
    }
  }

  object Verbalize {
    val digit = Map('0'->"zero",'1'->"one",'2'->"two",'3'->"three",'4'->"four",
                    '5'->"five",'6'->"six",'7'->"seven",'8'->"eight",'9'->"nine")
    def apply(n: Int) = n.toString.toList.map(c=>" "+digit(c)).mkString
  }

  class MeetingPlace(meetings: Int) {
    private val meetingsLeft = new atomic.AtomicInteger(meetings)
    private val creatureRef = new atomic.AtomicReference[Creature]

    def meet(incoming: Creature) {
      val existing = creatureRef.get
      if (existing == null) {
        if (!creatureRef.compareAndSet(existing,incoming)) meet(incoming);
      }
      else {
        val newColor = Color.doComplement(incoming.color,existing.color)      
        if (!creatureRef.compareAndSet(existing,null)) meet(incoming);
        else {
          meetingsLeft.getAndDecrement() match {
            case x if (x>0) =>
              existing.setColor(incoming.id, newColor, x==1);
              incoming.setColor(existing.id, newColor, x==1);
            case _ =>
              existing.finish()
              incoming.finish()
          }
        }
      }
    }
  }

  class Dispatcher(bqc: BlockingQueue[Creature]) extends Runnable {
    def run() {
      try {
        while(true) bqc.take().run()
      }
      catch { case e: InterruptedException => }
    }
  }

  class Creature(place: MeetingPlace, var color: Color.Value, bqc: BlockingQueue[Creature], latch: CountDownLatch) {
    val id = System.identityHashCode(this)
    var count = 0
    private var sameCount = 0

    def finish() { latch.countDown() }
    def setColor(id2: Int, newColor: Color.Value, complete: Boolean) {
      color = newColor
      count += 1
      if (id==id2) sameCount += 1
      if (!complete) bqc.add(this)
      else finish()
    }
    def run() { place.meet(this) }
    override def toString() = count + Verbalize(sameCount)
  }

  def run(n: Int, colors: Color.Value*) {
    val place = new MeetingPlace(n)
    val bqc = new ArrayBlockingQueue[Creature](colors.length)
    val latch = new CountDownLatch(colors.length - 1)

    val creatures = colors.map(c => { print(" "+c) ; new Creature(place, c, bqc, latch) })
    println

    val threads = colors.map(_ => new Thread(new Dispatcher(bqc)))
    threads.foreach(_.start())
    creatures.foreach(c => bqc.add(c))

    try { latch.await }
    catch { case ie: InterruptedException => println("Exiting with error: " + ie) }

    threads.foreach(_.interrupt())

    println( Verbalize ( (0 /: creatures)((sum,c) => { println(c) ; sum + c.count }) ) )
    println
  }

  def main(args: Array[String]) {
    import Color._

    val n = try { args(0).toInt } catch { case _ => 600 }
    
    printColors
    println

    run(n,Blue,Red,Yellow)
    run(n,Blue,Red,Yellow,Red,Yellow,Blue,Red,Yellow,Red,Blue)
  }
}

 make, command-line, and program output logs

Wed, 19 Mar 2014 06:34:28 GMT

MAKE:
mv chameneosredux.scala chameneosredux.scala
mv: ‘chameneosredux.scala’ and ‘chameneosredux.scala’ are the same file
make: [chameneosredux.scala_run] Error 1 (ignored)
/usr/local/src/scala-2.10.3/bin/scalac -optimise -target:jvm-1.7 chameneosredux.scala
chameneosredux.scala:33: error: value foreach is not a member of object chameneosredux.Color
      Color.foreach(c1 => Color.foreach(c2 => {
            ^
chameneosredux.scala:121: warning: This catches all Throwables. If this is really intended, use `case _ : Throwable` to clear this warning.
    val n = try { args(0).toInt } catch { case _ => 600 }
                                               ^
one warning found
one error found
make: [chameneosredux.scala_run] Error 1 (ignored)
2.36s 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 60000

PROGRAM FAILED 


PROGRAM OUTPUT:

Error: Could not find or load main class chameneosredux

Revised BSD license

  Home   Conclusions   License   Play