Benchmark Scala blocks of code with no pain.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Provides methods to measure and report the time used to execute Scala code.
Inspired by the Ruby Benchmark library.


  • Scala 2.9.1
  • sbt 0.11.0
  • Java SE JRE >6u25


git clone git://
cd Benchmark.scala
sbt package
cp target/scala-2.9.1/benchmark.scala_2.9.1-0.1.jar /path/to/project/lib

Example – Measurements

Measuring execution time to construct a 10M long string of letter “a”. Note that parenthesis can be omitted which helps readability.

println(Benchmark.measure { "a" * 10485760 })

This outputs the measurement below (run on a Core i7 Macbook Pro). This report shows, from left to right, the CPU time spent in user mode, the CPU time spent in system mode, the total CPU time (user mode or system mode) and real time in seconds.

    0.175734    0.000568    0.176302    0.215648

Example – Reports

Reports are measurements which belong to each other. Reports provide extra functionality like adding report headers and labels to each measurements. { x =>
  x.caption("Benchmarking the speed of creating a 10M long string of \"a\"") { "a" * 10485760 }

This outputs the following report. Column labels are automatically added.

Benchmarking the speed of creating a 10M long string of "a"
        user      system       total        real
    0.134755    0.000248    0.135003    0.168390

Example – Advanced reports

This example is taken form BenchmarkExample which calculates the 30th Fibonacci number using various algorithms.

val n = 30, { x =>
  x.caption("Calculating Fibonacci(%d) using different algorithms:".format(n))"Recursive:", { FibonacciRecursive.fib(n) })"Iterative:", { FibonacciIterative.fib(n) })"Tail recursive:", { FibonacciTailRecusive.fib(n) })"Functional stream:", { FibonacciFunctionalStream.fib(n) })"Fold left:", { FibonacciFoldLeft.fib(n) })

This outputs the following report.

Calculating Fibonacci(30) using different algorithms:
                            user      system       total        real
Recursive:              0.010270    0.000185    0.010455    0.010713
Iterative:              0.000441    0.000065    0.000506    0.000505
Tail recursive:         0.000009    0.000004    0.000013    0.000011
Functional stream:      0.000685    0.000110    0.000795    0.000805
Fold left:              0.000696    0.000112    0.000808    0.000817
Total:                  0.012101    0.000476    0.012577    0.012851
Avg:                    0.002420    0.000095    0.002515    0.002570