Consider the following two code blocks, http://ideone.com/3nNdVs
String[] matches = new String[] {"Foo", "Bar"};
long start = System.nanoTime();
for(int i=0; i< 1000000; i++) {
String name = "This String is Foo Bar";
for (String s : matches){
name = name.replace(s, "");
}
}
System.out.println((System.nanoTime() - start)/1000000);
matches = {"Foo", "Bar"}
start = time.time()
for x in xrange(1000000):
name = "This String is Foo Bar"
for s in matches:
name = name.replace(s, "")
print time.time() - start
While trying to benchmark the performance of these two, I found that the one implemented in Java takes about 50% longer than the Python. This came as quite a shock to me as i was expecting the Python version to be slower.
So the first question is, are there better or faster ways to perform these two functions?
Second, if not, why is the Java version slower than the Python one?
I found out the reason that python was quicker, it is because the .replace method in java uses regex which is compiled every time you call .replace.
there are many quicker alternatives, but the one that i found to be most convinient is to use org.apache.commons.lang3.StringUtils library's .replaceEach which uses index of to find and replace substrings which i understand is still faster than a one time compiled regex.
long start = System.nanoTime();
for(int i=0; i< 1000000; i++) {
String name = "This String is Foo Bar";
name = StringUtils.replaceEach(name, matches, replaces);
}
System.out.println((System.nanoTime() - start)/1000000);
unfortunatly i cany provide a link on ide one as they dont have apache commons.
This version of the algorithm on my system was about 1/4 faster than the .replace method and about 1/2 faster than the python.
if anyone has a faster option for python let me know
thanks
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments