python split() 与 rsplit() 性能? [英] python split() vs rsplit() performance?

查看:36
本文介绍了python split() 与 rsplit() 性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中有一个字符串.我想用 maxsplit = 1 在非常接近字符串末尾的分隔符上分割它.

例如

a = "abcdefghijklmnopqrstuvwxyz,1".

a.split(",", 1) 在性能方面会比 a.rsplit(",",1) 更好吗?

解决方案

以下是使用 timeit.timeit 比较两种方法的速度:

<预><代码>>>>从时间导入时间>>>timeit('"abcdefghijklmnopqrstuvwxyz,1".split(",", 1)')1.6438178595324267>>>timeit('"abcdefghijklmnopqrstuvwxyz,1".rsplit(",", 1)')1.6466740884665505>>>

如您所见,它们大致相同.str.split 快了几分之一秒,但这真的不重要.所以,你可以选择任何你想要的方法.

附言虽然,str.split 方法 少了一个要输入的字符.:)

I have a string in python. I want to split it with maxsplit = 1 on separator which is pretty close to end of the string.

For e.g.

a = "abcdefghijklmnopqrstuvwxyz,1".

Will a.split(",", 1) be better in terms of performance than a.rsplit(",",1)?

解决方案

Below is a time test using timeit.timeit to compare the speeds of the two methods:

>>> 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. So, you can pick whichever method you want.

P.S. Although, the str.split method is one less character to type. :)

这篇关于python split() 与 rsplit() 性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆