Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这些字符串操作在Java和Python中等效吗?_Java_Python_String_Performance_Replace - Fatal编程技术网

这些字符串操作在Java和Python中等效吗?

这些字符串操作在Java和Python中等效吗?,java,python,string,performance,replace,Java,Python,String,Performance,Replace,考虑以下两个代码块, 在尝试对这两种方法的性能进行基准测试时,我发现用Java实现的方法比Python要花费大约50%的时间。这对我来说是一个相当大的冲击,因为我期望Python版本会更慢 因此,第一个问题是,有更好或更快的方法来执行这两个功能吗 其次,如果不是,为什么Java版本比Python版本慢?对于Python,请使用timeit模块: import timeit setup = """ matches = {'Foo', 'Bar'} for x in xrange(1000000

考虑以下两个代码块,

在尝试对这两种方法的性能进行基准测试时,我发现用Java实现的方法比Python要花费大约50%的时间。这对我来说是一个相当大的冲击,因为我期望Python版本会更慢

因此,第一个问题是,有更好或更快的方法来执行这两个功能吗


其次,如果不是,为什么Java版本比Python版本慢?

对于Python,请使用timeit模块:

import timeit

setup = """
matches = {'Foo', 'Bar'}
for x in xrange(1000000):
  name = 'This String is Foo Bar'
  for s in matches:
    name = name.replace(s, '')
"""

print min(timeit.Timer(setup=setup).repeat(10))

我发现python之所以更快,是因为java中的.replace方法使用regex,每次调用.replace时都会编译regex

有许多更快的替代方法,但我发现最方便的方法是使用org.apache.commons.lang3.StringUtils库的.replaceAch,它使用的索引查找和替换子字符串,据我所知,仍然比一次性编译的正则表达式快

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);
long start=System.nanoTime();
对于(int i=0;i<1000000;i++){
String name=“这个字符串是Foo-Bar”;
name=StringUtils.replaceach(名称、匹配项、替换项);
}
System.out.println((System.nanoTime()-start)/1000000);
不幸的是,我无法在ide one上提供链接,因为他们没有apache commons

在我的系统上,这个版本的算法比.replace方法快约1/4,比python快约1/2

如果有人有更快的python选项,请告诉我


谢谢

“这些字符串操作在Java和Python中是等价的吗?”嗯……不……Python版本进行替换(
匹配
有“Foo”和“Bar”),Java版本没有(
匹配
没有“Foo”或“Bar”)。顺便说一句,集合上的迭代比列表和元组慢,并且您的代码不等价。
匹配={“Foo”,“Bar”}
是一个集合;您几乎肯定需要一个列表:
匹配=[“Foo”,“Bar”]
。语言没有速度,它们只有语义。如果要比较速度,必须选择要比较的特定实现。如果要在基于*nix的计算机上进行性能测试,以了解更多信息,可以使用perf stat-B(sudo apt get install linux tools common linux base)我将链接到这一点,因为它似乎是相关的:对不起,Jeff,但我不知道这是否真的能帮助我更接近一个答案。@user779420:Jeff在一个方面是完全正确的:您正在测量一些与实际性能无关的时间。在Java中,您需要类似于或当您想要获得与实际性能相关的数字时。strings是不可变的。每次执行循环时,您都在创建字符串的副本。s.replace也会创建一个副本。以下操作始终表现得更好:“”。join([x代表x在“此字符串”中为Foo-Bar)。split()如果x不在(“Foo”,“Bar”))@Jeff Bond实际上,在我看来,替换比连接快,这就是Java one速度慢的原因。如果在编译时知道要替换的正则表达式是什么,可以使用Pattern.compile(“Foo”)、Pattern.compile(“Bar”)为了节省一些时间。@theSilentOne是的,你是对的,但我认为indexof解决方案仍然会比编译的正则表达式快。如果字符串更长或更复杂,那么正则表达式无疑是一个不错的选择
import timeit

setup = """
matches = {'Foo', 'Bar'}
for x in xrange(1000000):
  name = 'This String is Foo Bar'
  for s in matches:
    name = name.replace(s, '')
"""

print min(timeit.Timer(setup=setup).repeat(10))
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);