Skip to content

Instantly share code, notes, and snippets.

@jrlemieux
Created January 23, 2012 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrlemieux/1665557 to your computer and use it in GitHub Desktop.
Save jrlemieux/1665557 to your computer and use it in GitHub Desktop.
Very strange
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.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment