简体   繁体   English

python split() 与 rsplit() 性能?

[英]python split() vs rsplit() performance?

I have a string in python.我在python中有一个字符串。 I want to split it with maxsplit = 1 on separator which is pretty close to end of the string.我想在非常接近字符串末尾的分隔符上使用maxsplit = 1拆分它。

For eg例如

a = "abcdefghijklmnopqrstuvwxyz,1".

Will a.split(",", 1) be better in terms of performance than a.rsplit(",",1) ? a.split(",", 1)在性能方面会比a.rsplit(",",1)更好吗?

Below is a time test using timeit.timeit to compare the speeds of the two methods:下面是使用timeit.timeit来比较两种方法的速度的时间测试:

>>> from timeit import timeit
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)')
1.6438178595324267
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)')
1.6466740884665505
>>>

As you can see, they are about equivalent.如您所见,它们大致相同。 str.split is a few fractions of a second faster, but that is really unimportant. str.split快了几分之一秒,但这真的不重要。 So, you can pick whichever method you want.所以,你可以选择你想要的任何方法。

PS Although, the str.split method is one less character to type. PS虽然该str.split方法一个键入字符少。 :) :)

I'm super late to this party, but for anyone else stumbling across this, partition is faster than split(x, 1) :我参加这个聚会超级晚,但是对于其他遇到此问题的人来说, partitionsplit(x, 1)快:

>>> from timeit import timeit
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)')
0.23717808723449707
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)')
0.20203804969787598
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".partition(",")')
0.11137795448303223
>>> timeit('"abcdefghijklmnopqrstuvwxyz,1".rpartition(",")')
0.10027790069580078

And you can ditch the , easily if you want by h, _, t = s.rpartition(',') or such.如果你愿意,你可以很容易地通过h, _, t = s.rpartition(',')等来h, _, t = s.rpartition(',')

I think there is a slight difference between split() and rsplit() : for example:我认为split()rsplit()之间存在细微差别:例如:

str1 = "w,e,l,c,o,m,e"
print(str1.split(',',2))

str1 = "w,e,l,c,o,m,e"
print(str1.rsplit(',',2))

You see, split() is used if you want to split strings on first occurrences and rsplit() is used if you want to split strings on last occurrences.你看, split()的使用,如果你想在第一事件和分裂字符串rsplit()如果你想在最后出现的字符串分割使用。

Adding to the previous answers, using split vs rsplit should depend on where you want to search.添加到前面的答案中,使用 split 还是 rsplit 应该取决于您要搜索的位置。 Example:例子:

$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,sdfsgfkdjgherughieug,1".split(",")[2]'
1000000 loops, best of 3: 0.48 usec per loop
$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,sdfsgfkdjgherughieug,1".rsplit(",",1)[1]'
1000000 loops, best of 3: 0.453 usec per loop

Here you are searching for 1, in which case using rsplit is faster than split, whereas for the examples in the previous answers, split is faster.在这里,您正在搜索 1,在这种情况下,使用 rsplit 比 split 快,而对于前面答案中的示例,split 更快。

Just to complement @iCodez answer, you can run a timing test from the command-line:为了补充@iCodez 的答案,您可以从命令行运行计时测试:

$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)'
1000000 loops, best of 3: 0.321 usec per loop
$ python -m timeit '"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)'
1000000 loops, best of 3: 0.327 usec per loop

So, indeed, it's an irrelevant difference.所以,实际上,这是一个无关紧要的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM