Are these String operations equivalent in Java and Python?

user779420

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

and http://ideone.com/v8wg6m

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?

user779420

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python string 'Template' equivalent in java

From Dev

Python string 'Template' equivalent in java

From Dev

Equivalent java regex string for this

From Dev

Equivalent operations on instance of Ruby class (to Java)

From Dev

UTF-8 string to ordinal value: Java equivalent for Python output

From Dev

Java equivalent to python's "with"

From Java

An equivalent to Java volatile in Python

From Dev

Equivalent for ? in Java for Python?

From Dev

java equivalent to python for hashing

From Dev

Python equivalent of Java Comparator

From Dev

An equivalent to Java volatile in Python

From Dev

Java equivalent of Python dictionary

From Dev

JavaScript equivalent of python string slicing

From Dev

PHP equivalent for python string slice

From Dev

Python 3 Equivalent of '%' String Operator

From Dev

Python : Java throws equivalent in python

From Dev

String case operations in CSVs, Python

From Dev

Java / Kotlin equivalent of Swift [String : [String : Any]]

From Dev

Java equivalent to python all and any

From Dev

Java equivalent of python nested for loop

From Dev

Python Equivalent of Java's 'Keystore'?

From Dev

Java equivalent for Python's toordinal()?

From Dev

Python equivalent of Java abstract classes?

From Dev

java's getByte() equivalent in python

From Dev

What is the Python equivalent of RequestConfig in Java?

From Dev

Which is the equivalent of "isinstance" (Python) for Java?

From Dev

Python equivalent of Java abstract classes?

From Dev

java's getByte() equivalent in python

From Dev

Is this Python code equivalent of Java code?