Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Benchmark Scala blocks of code with no pain.
Scala
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src/main/scala/com/balagez
.gitignore
LICENSE
README.textile
build.sbt

README.textile

Benchmark.scala

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

Requirements

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

Installation

git clone git://github.com/balagez/Benchmark.scala.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.

Benchmark.bm { x =>
  x.caption("Benchmarking the speed of creating a 10M long string of \"a\"")
  x.report { "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
Benchmark.bm(20, { x =>
  x.caption("Calculating Fibonacci(%d) using different algorithms:".format(n))
  x.report("Recursive:", { FibonacciRecursive.fib(n) })
  x.report("Iterative:", { FibonacciIterative.fib(n) })
  x.report("Tail recursive:", { FibonacciTailRecusive.fib(n) })
  x.report("Functional stream:", { FibonacciFunctionalStream.fib(n) })
  x.report("Fold left:", { FibonacciFoldLeft.fib(n) })
  x.separator
  x.total()
  x.avg()
}

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
Something went wrong with that request. Please try again.