Giter Site home page Giter Site logo

benchmark.scala's Introduction

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

benchmark.scala's People

Contributors

balagez avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.