在 Python 中连接字符串的最有效方法 [英] Most Efficient Method to Concatenate Strings in Python

查看:51
本文介绍了在 Python 中连接字符串的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在提出这个问题时,我使用的是 Python 3.8

当我说高效时,我指的只是字符串连接的速度,或者用更专业的术语:我问的是时间复杂度,而不是空间复杂度.

目前我能想到的唯一方法是以下 3 种:

a = '开始'b = '结束'

方法一

result = a + b

方法二

result = ''.join((a, b))

方法三

result = '{0}{1}'.format(a, b)

我想知道这些方法哪个更快,或者是否有其他方法更有效.此外,如果您知道这些方法中的任何一种在处理更多字符串或更长字符串时的表现是否不同,请将其包含在您的答案中.

编辑

在看到所有的评论和答案后,我学到了一些连接字符串的新方法,并且我还了解了 timeit 库.我将在下面报告我的个人发现:

<预><代码>>>>导入时间>>>print(timeit.Timer('result = a + b', setup='a = "start"; b = " end"').timeit(number=10000))0.0005306000000473432>>>print(timeit.Timer('result = "".join((a, b))', setup='a = "start"; b = " end"').timeit(number=10000))0.0011297000000354274>>>print(timeit.Timer('result = "{0}{1}".format(a, b)', setup='a = "start"; b = " end"').timeit(number=10000))0.002327799999989111>>>print(timeit.Timer('result = f"{a}{b}"', setup='a = "start"; b = " end"').timeit(number=10000))0.0005772000000092703>>>print(timeit.Timer('result = "%s%s" % (a, b)', setup='a = "start"; b = " end"').timeit(number=10000))0.0017815999999584164

似乎对于这些小字符串,传统的a + b 方法进行字符串拼接是最快的.谢谢大家的回答!

解决方案

为什么不试试呢?您可以使用 timeit.timeit() 来运行语句多次返回总时长.

这里,我们使用s设置变量ab(不计入总时间),然后运行各种选项 1000 万次.

<预><代码>>>>从时间导入时间>>>>>>n = 10 * 1000 * 1000>>>s = "a = '开始'; b = '结束'">>>>>>timeit("c = a + b", setup=s, number=n)0.4452877212315798>>>>>>timeit("c = f'{a}{b}'", setup=s, number=n)0.5252049304544926>>>>>>timeit("c = '%s%s'.format(a, b)", setup=s, number=n)0.6849184390157461>>>>>>>timeit("c = ''.join((a, b))", setup=s, number=n)0.8546998891979456>>>>>>timeit("c = '%s%s' % (a, b)", setup=s, number=n)1.1699129864573479>>>>>>timeit("c = '{0}{1}'.format(a, b)", setup=s, number=n)1.5954962372779846

这表明除非您的应用程序的瓶颈是字符串连接,否则可能不值得太担心...

  • 最佳情况是 1000 万次迭代约 0.45 秒,或每次操作约 45 纳秒.
  • 最坏的情况是 1000 万次迭代约 1.59 秒,或每次操作约 159 纳秒.

如果您正在执行数百万次的操作,您会发现速度提高了大约 1 秒.

请注意,根据您连接的字符串的长度(和数量)以及运行的硬件,您的结果可能会有很大差异.

At the time of asking this question, I'm using Python 3.8

When I say efficient, I'm only referring to the speed at which the strings are concatenated, or in more technical terms: I'm asking about the time complexity, not accounting the space complexity.

The only methods I can think of at the moment are the following 3 given that:

a = 'start'
b = ' end'

Method 1

result = a + b

Method 2

result = ''.join((a, b))

Method 3

result = '{0}{1}'.format(a, b)

I want to know which of these methods are faster, or if there are other methods that are more efficient. Also, if you know if either of these methods performs differently with more strings or longer strings, please include that in your answer.

Edit

After seeing all the comments and answers, I have learned a couple of new ways to concatenate strings, and I have also learned about the timeit library. I will report my personal findings below:

>>> import timeit

>>> print(timeit.Timer('result = a + b', setup='a = "start"; b = " end"').timeit(number=10000))
0.0005306000000473432

>>> print(timeit.Timer('result = "".join((a, b))', setup='a = "start"; b = " end"').timeit(number=10000))
0.0011297000000354274

>>> print(timeit.Timer('result = "{0}{1}".format(a, b)', setup='a = "start"; b = " end"').timeit(number=10000))
0.002327799999989111

>>> print(timeit.Timer('result = f"{a}{b}"', setup='a = "start"; b = " end"').timeit(number=10000))
0.0005772000000092703

>>> print(timeit.Timer('result = "%s%s" % (a, b)', setup='a = "start"; b = " end"').timeit(number=10000))
0.0017815999999584164

It seems that for these small strings, the traditional a + b method is the fastest for string concatenation. Thanks for all of the answers!

解决方案

Why don't you try it out? You can use timeit.timeit() to run a statement many times and return the overall duration.

Here, we use s to setup the variables a and b (not included in the overall time), and then run the various options 10 million times.

>>> from timeit import timeit
>>>
>>> n = 10 * 1000 * 1000
>>> s = "a = 'start'; b = ' end'"
>>>
>>> timeit("c = a + b",                 setup=s, number=n)
0.4452877212315798
>>>
>>> timeit("c = f'{a}{b}'",             setup=s, number=n)
0.5252049304544926
>>>
>>> timeit("c = '%s%s'.format(a, b)",   setup=s, number=n)
0.6849184390157461
>>>>
>>> timeit("c = ''.join((a, b))",       setup=s, number=n)
0.8546998891979456
>>>
>>> timeit("c = '%s%s' % (a, b)",       setup=s, number=n)
1.1699129864573479
>>>
>>> timeit("c = '{0}{1}'.format(a, b)", setup=s, number=n)
1.5954962372779846

This shows that unless your application's bottleneck is string concatenation, it's probably not worth being too concerned about...

  • The best case is ~0.45 seconds for 10 million iterations, or about 45ns per operation.
  • The worst case is ~1.59 seconds for 10 million iterations, or about 159ns per operation.

If you're performing literally millions of operations, you'll see a speed improvement of about 1 second.

Note that your results may vary quite drastically depending on the lengths (and number) of the strings you're concatenating, and the hardware you're running on.

这篇关于在 Python 中连接字符串的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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