Skip to content

Instantly share code, notes, and snippets.

@dk8996
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dk8996/8b7a96d2a14c7a664820 to your computer and use it in GitHub Desktop.
Save dk8996/8b7a96d2a14c7a664820 to your computer and use it in GitHub Desktop.
Bigrams, Scala vs Java
public class Util {
public static Set < String > toBigramsJava(String s1) {
Set < String > nx = new HashSet < String > ();
for (int i = 0; i < s1.length() - 1; i++) {
char x1 = s1.charAt(i);
char x2 = s1.charAt(i + 1);
String tmp = "" + x1 + x2;
nx.add(tmp);
}
return nx;
}
}
object Util {
def toBigramsScala(str: String): scala.collection.mutable.Set[String] = {
val hash: scala.collection.mutable.Set[String] = scala.collection.mutable.HashSet[String]()
for (i < -0 to str.length - 2) {
val x1 = str.charAt(i)
val x2 = str.charAt(i + 1)
val tmp = "" + x1 + x2
hash.add(tmp)
}
return hash
}
}
@dk8996
Copy link
Author

dk8996 commented Aug 31, 2014

Performance Results for blog post:
http://www.mentful.com/2014/08/31/generating-bigrams-performance-scala-vs-java/

Scala version (about 1985 ms)

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsScala("test test abc de")})
17:00:05.034 [info] Something took: 1985ms

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsScala("test test abc de")})
17:00:08.417 [info] Something took: 1946ms

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsScala("test test abc de")})
17:00:11.452 [info] Something took: 1970ms

Java version (about 623 ms)

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsJava("test test abc de")})
17:01:51.597 [info] Something took: 623ms

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsJava("test test abc de")})
17:01:53.094 [info] Something took: 620ms

scala> Util.time(for(i<-1 to 1000000) {Util.toBigramsJava("test test abc de")})
17:01:54.519 [info] Something took: 606ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment