为什么这个Scala代码会变慢?

sin*_*yma 7 performance json scala

我正在运行以下Scala代码:

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
                }
        }
}
Run Code Online (Sandbox Code Playgroud)

在我的机器上,http://sortable.com/blog/coding-challenge/上的文件大约需要3分钟.我编写的等效Haskell和Ruby程序在4秒内完成.我究竟做错了什么?

我尝试了没有地图的相同代码(jsonStringMap)并且速度非常快,JSON解析器真的很慢吗?

看起来很可能默认的JSON解析器真的很慢,但我尝试了https://github.com/stevej/scala-json,虽然它降低到35秒,但仍然比Ruby慢得多.

我现在使用的是https://github.com/codahale/jerkson,速度更快!我的程序现在只在我的数据上运行6秒,比Ruby慢3秒,这可能只是JVM的启动.

huy*_*hjl 8

快速查看scala-user存档似乎表明没有人在scala标准库中使用JSON解析器进行认真的工作.

请参阅http://groups.google.com/group/scala-user/msg/fba208f2d3c08936

看起来解析器最终出现在标准库中,当时斯卡拉不太受欢迎,并且没有今天的期望.