问题描述:

I am trying to figure it out if there is a faster way to get the content from a URL Using Scala. Using Apache IOUtils I am able to get the content faster than Source.fromURL in Scala. Here is the code from both:

Using 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();

}

Using Scala:

 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("\""))

In Java it takes around: 155899 ns

In Scala : 343880 ns

Why is the Scala code so slow?

网友答案:

Because in your first you're reading from a buffer, while in your second you're reading line by line.

Source.getLines is not intended as a silver bullet replacement for IOUtils or whatever other library, it's intended to read line by line. You can perfectly use IOUtils in Scala.

网友答案:

Looking at how slow your connection is (between 2 and 6 minutes to get gizmodo), you may find that the reading speed between Scala and Java is not the problem, but rather an unreliable internet connection. If you really want to test speed differences, then try serving a file from a local server so that you scrap internet connection speed from the equation.

Lastly, for an advanced HTTP client, I would look at Spray HTTP Client. Spray is a really nice non-blocking (using Actors) HTTP toolset.

相关阅读:
Top