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?
matcheshas "Foo" and "Bar"), the Java version doesn't (matchesdoes not have "Foo" or "Bar").matches = {"Foo", "Bar"}is a set; you almost certainly want a list:matches = ["Foo", "Bar"].