public
Created

Very strange

  • Download Gist
Strange.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
object Strange {
 
def fmt(x: Double) = x.formatted("%.2f")
 
def run(f: => Any) = {
val n = 100000000L
val start = System.nanoTime
var i = 0L
while (i < n) {
f
i += 1
}
val stop = System.nanoTime
val delta = (stop - start).toDouble / 1000000000.0
val perSecond = n.toDouble / delta
val millionsPerSecond = perSecond / 1000000.0
println(fmt(millionsPerSecond) + " millions per second (for " + fmt(delta) + " seconds).")
}
 
def main(args: Array[String]) = {
args match {
case Array("A") =>
for (i <- 0 until 12) {
run(1 + 1)
}
case Array("B") =>
for (i <- 0 until 6) {
run(1 + 1)
run(1 + 1)
}
case Array("C") =>
for (i <- 0 until 4) {
run(1 + 1)
run(1 + 1)
run(1 + 1)
}
case Array("D") =>
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
run(1 + 1)
case Array("E") =>
def go = run(1 + 1)
go
go
go
go
go
go
go
go
go
go
go
go
case _ =>
println("Bad input, use A, B, C, D or E.")
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.