为什么此Scala代码运行缓慢? [英] Why is this Scala code slow?
问题描述
我正在运行以下Scala代码:
I'm running 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
}
}
}
在我的计算机上,从 http://sortable.com/blog/coding-challenge/获取的文件大约需要3分钟.我编写的等效Haskell和Ruby程序用时不到4秒.我在做什么错了?
On my machine it takes ~3 minutes on the file from http://sortable.com/blog/coding-challenge/. Equivalent Haskell and Ruby programs I wrote take under 4 seconds. What am I doing wrong?
我在没有map(jsonStringMap)的情况下尝试了相同的代码,而且速度非常快,所以JSON解析器真的很慢吗?
I tried the same code without the map(jsonStringMap) and it was plenty fast, so is the JSON parser just really slow?
默认JSON解析器似乎确实很慢,但是我尝试了 https://github.com/stevej/scala-json ,虽然将其缩短到35秒,但仍然比Ruby慢得多.
It does seem likely that the default JSON parser is just really slow, however I tried https://github.com/stevej/scala-json and while that gets it down to 35 seconds, that's still much slower than Ruby.
我现在正在使用 https://github.com/codahale/jerkson ,它甚至更快[em] !现在,我的程序仅对数据运行6秒钟,仅比Ruby慢3秒钟,而Ruby可能只是JVM的启动.
I am now using https://github.com/codahale/jerkson which is even faster! My program now runs in only 6 seconds on my data, only 3 seconds slower than Ruby, which is probably just the JVM starting up.
推荐答案
快速浏览scala用户档案似乎表明没有人对scala标准库中的JSON解析器进行过认真的工作.
A quick look at the scala-user archive seems to indicate that nobody is doing serious work with the JSON parser in the scala standard library.
请参见 http://groups.google.com/group/scala-user /msg/fba208f2d3c08936
似乎解析器最终出现在标准库中,而此时scala并不是人们关注的焦点,并且没有今天的期望.
It seems the parser ended up in the standard library at a time when scala was less in the spotlight and didn't have the expectations it has today.
这篇关于为什么此Scala代码运行缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!