Python:为什么partition(sep)比split(sep,maxsplit = 1)快 [英] Python: why partition(sep) is faster than split(sep, maxsplit=1)

查看:378
本文介绍了Python:为什么partition(sep)比split(sep,maxsplit = 1)快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个有趣的事情是,在分隔符之后获取整个子字符串时,分区 split 快。我已经在Python 3.5和3.6(Cpython)中进行了测试

I found an interesting thing that partition is faster than split when get whole substring after the separator. I have tested in Python 3.5 and 3.6 (Cpython)

In [1]: s = 'validate_field_name'

In [2]: s.partition('_')[-1]
Out[2]: 'field_name'

In [3]: s.split('_', maxsplit=1)[-1]
Out[3]: 'field_name'

In [4]: %timeit s.partition('_')[-1]
220 ns ± 1.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: %timeit s.split('_', maxsplit=1)[-1]
745 ns ± 48.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [6]: %timeit s[s.find('_')+1:]
340 ns ± 1.44 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

我浏览了Cpython源代码,发现分区使用 FASTSEARCH 算法,请参见这里。并且 split 仅在分隔符字符串的长度大于1时使用 FASTSEARCH ,请参见此处。但是我已经测试了长度更大的sep字符串。我得到了相同的结果。

I look through the Cpython source code and found the partition use the FASTSEARCH algorithm, see here. And the split only use FASTSEARCH when the separator string's length is larger than 1, see here. But I have tested on sep string which length is larger. I got same result.

我想原因是 partition 返回了三个元素元组,而不是列表。

I guess the reason is partition return a three elements tuple, instead of a list.

我想了解更多详细信息。

I want to know more details.

推荐答案

微基准测试可以误导性

py -m timeit "'validate_field_name'.split('_', maxsplit=1)[-1]"
1000000 loops, best of 3: 0.568 usec per loop

py -m timeit "'validate_field_name'.split('_', 1)[-1]"
1000000 loops, best of 3: 0.317 usec per loop

仅将参数传递为位置或关键字会明显改变时间。所以我想分区的另一个原因是分区更快,因为它不需要第二个参数...

Just passing the argument as positional or keyword changes the time significantly. So I would guess another reason partition is faster, because it does not need a second argument...

这篇关于Python:为什么partition(sep)比split(sep,maxsplit = 1)快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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