Created
October 23, 2012 04:10
-
-
Save mattetti/3936587 to your computer and use it in GitHub Desktop.
quick comparison in processing collections in Ruby vs Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Scala: 787ms
jRuby 1.7 (JDK 7): 3328.0ms
You can shave off ~15% by using Array#sample instead of rand(26) :-)
I know this is really old, but it's a top Google result so thought I'd share. Using a solid benchmarking tool like Caliper to allow for JVM warmup will produce results about 200X faster for the Scala version: https://github.com/sam/collections-vs (<4ms instead of ~800ms).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not your usual scala code but I was just curious how collection performed in scala vs jruby.