Last active
August 29, 2015 14:05
-
-
Save dk8996/8b7a96d2a14c7a664820 to your computer and use it in GitHub Desktop.
Bigrams, Scala vs Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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