I run the following Scala code:
import scala.util.parsing.json._ import scala.io._ object Main { def jsonStringMap(str: String) = JSON.parseFull(str) match { case Some(m: Map[_,_]) => m collect { // If this doesn't match, we'll just ignore the value case (k: String, v: String) => (k,v) } toMap case _ => Map[String,String]() } def main(args: Array[String]) { val fh = Source.fromFile("listings.txt") try { fh.getLines map(jsonStringMap) foreach { v => println(v) } } finally { fh.close } } }
On my machine, it takes ~ 3 minutes in a file from http://sortable.com/blog/coding-challenge/ . The Haskell and Ruby equivalent programs I wrote take less than 4 seconds. What am I doing wrong?
I tried the same code without a map (jsonStringMap) and it was very fast, just like a JSON parser is just very slow?
It seems likely that the default JSON parser is just very slow, however I tried https://github.com/stevej/scala-json , and as long as it reaches 35 seconds, it's still a lot slower than Ruby.
Now I am using https://github.com/codahale/jerkson , which is even faster! My program now only works for 6 seconds according to my data, only 3 seconds slower than Ruby, which is probably just starting the JVM.
singpolyma
source share