我想弄明白,如果有一种更快的方式从URL使用Scala获取内容。使用Apache IOUtils
我能够在Scala中获得比Source.fromURL
更快的内容。下面是从两个密码:为什么Scala Source.fromURL比Apaches IOUtils慢?
使用Java:
try {
tmp=IOUtils.toString(new URL("http://gizmodo.com"));
tmp=tmp.substring(tmp.indexOf("360\" title=")+12);
tmp=tmp.substring(0,tmp.indexOf("\""));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
使用斯卡拉:
val gizmodoPageLines = Source.fromURL("http://gizmodo.com").getLines.mkString("\n")
val pos=gizmodoPageLines.indexOf("360\" title=")
var tmp=gizmodoPageLines.substring(pos+12)
tmp.substring(0,tmp.indexOf("\""))
在Java约需:155899纳秒
在斯卡拉:343880纳秒
为什么Scala代码如此之慢?
它真的让你花费数百秒来拉下gizmodo.com的首页内容? –
你有没有试过[scala-io](http://jesseeichar.github.com/scala-io-doc/0.4.1/index.html#!/performance/Class-scalax.io.perf.channel.input。 SmallMediumSetsFromFileReadableByteChannelTest_Input-字节阵列)?该网站上有一些很好的性能数据。 – AmigoNico