Skip to content

Instantly share code, notes, and snippets.

@mauricio
Forked from mattetti/collection.rb
Created October 24, 2012 05:15
Show Gist options
  • Save mauricio/3944104 to your computer and use it in GitHub Desktop.
Save mauricio/3944104 to your computer and use it in GitHub Desktop.
quick comparison in processing collections in Ruby vs Scala
import scala.util.Random
import scala.collection.mutable.ArrayBuffer
import scala.annotation.tailrec
import scala.collection.immutable.Vector
object Benchmark {
@tailrec def repeat(n: Int)(f: => Unit) {
if (n > 0) { f; repeat(n - 1)(f) }
}
def main(args: Array[String]) {
val start = System.currentTimeMillis
repeat(100) {
val letterList = new ArrayBuffer[Char]()
val alphabet = ('a' until 'z').toArray
repeat(100000) { letterList.append( alphabet(Random.nextInt(25)) ) }
val distinct = letterList.distinct
val pick = distinct(Random.nextInt(distinct.size))
val (before, after) = letterList.partition { _ > pick }
}
println(System.currentTimeMillis - start)
}
}
start = Time.now
100.times do
letter_list = []
alphabet = ('a'..'z').to_a
100_000.times{ letter_list << alphabet[rand(26)] }
pick = letter_list.uniq[rand(26)]
before, after = letter_list.partition{|n| n > pick}
end
p (Time.now - start) * 1000
import scala.util.Random
val start = System.currentTimeMillis
(1 to 100).foreach { _ =>
val alphabet = ('a' to 'z').toArray
val letterList = (0 to 100000).map( _ => alphabet(Random.nextInt(26)) )
val pick = letterList.distinct(Random.nextInt(26))
val (before, after) = letterList.partition{ _ > pick }
}
println(System.currentTimeMillis - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment