public
Created

Benchmarking for some factorial functions using Caliper

  • Download Gist
FactorialBenchmark.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
import org.jscience.mathematics.number.LargeInteger
 
import com.google.caliper.{ Param, SimpleBenchmark }
 
class FactorialBenchmark extends SimpleBenchmark {
 
// Borrowed from https://github.com/sirthias/scala-benchmarking-template.
def repeat[@specialized A](reps: Int)(snippet: => A) = {
val zero = 0.asInstanceOf[A]
var i = 0
var result = zero
while (i < reps) {
val res = snippet
if (res != zero) result = res
i = i + 1
}
result
}
 
def timeBigIntFoldLeft(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).foldLeft(BigInt(1))(_ * _)
}
 
def timeBigIntFold(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).fold(BigInt(1))(_ * _)
}
 
def timeBigIntReduce(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).reduce(_ * _)
}
 
def timeBigIntFoldLeftPar(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).par.foldLeft(BigInt(1))(_ * _)
}
 
def timeBigIntFoldPar(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).par.fold(BigInt(1))(_ * _)
}
 
def timeBigIntReducePar(reps: Int) = repeat(reps) {
(BigInt(1) to BigInt(50000)).par.reduce(_ * _)
}
 
def timeLargeIntegerFoldLeft(reps: Int) = repeat(reps) {
(1 to 50000).foldLeft(LargeInteger.ONE)(_ times _)
}
 
def timeLargeIntegerFold(reps: Int) = repeat(reps) {
(1 to 50000).map(LargeInteger.valueOf(_)).fold(LargeInteger.ONE)(_ times _)
}
 
def timeLargeIntegerReduce(reps: Int) = repeat(reps) {
(1 to 50000).map(LargeInteger.valueOf(_)).reduce(_ times _)
}
 
def timeLargeIntegerFoldLeftPar(reps: Int) = repeat(reps) {
(1 to 50000).par.foldLeft(LargeInteger.ONE)(_ times _)
}
 
def timeLargeIntegerFoldPar(reps: Int) = repeat(reps) {
(1 to 50000).map(LargeInteger.valueOf(_)).par.fold(LargeInteger.ONE)(_ times _)
}
 
def timeLargeIntegerReducePar(reps: Int) = repeat(reps) {
(1 to 50000).map(LargeInteger.valueOf(_)).par.reduce(_ times _)
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.