The three methods of python to realize string concatenation and their efficiency
- 2020-05-19 05:13:40
- OfStack
python string concatenation methods, 1 generally have the following three kinds:
Method 1: connect directly via the plus (+) operator
website = 'python' + 'tab' + '.com'
Method 2: join method
listStr = ['python', 'tab', '.com']
website = ''.join(listStr)
Method 3: substitution
website = '%s%s%s' % ('python', 'tab', '.com')
Now let's talk about the difference between 1 and 3
Method 1 is simple and straightforward to use, but many people on the Internet say this method is inefficient
The reason why the operation of string concatenation using + in python is inefficient is that the string in python is an immutable type. When using + to concatenate two strings, a new string will be generated, and a new string will need to be reapplied to memory to generate a new string. When many strings are added continuously (a+b+c+d+e+f+...) Inefficiency is inevitable
Method 2 is slightly more complex, but efficient when connecting multiple characters, with only one memory request. And if you are concatenating list characters, this method must be preferred
Method 3: string formatting. This method is very common and I recommend it
The following experiment illustrates the efficiency of string concatenation.
Comparison object: plus connection VS join connection
python version: python 2.7
System environment: CentOS
Experiment 1:
# -*- coding: utf-8 -*-
from time import time
def method1():
t = time()
for i in xrange(100000):
s = 'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'+'pythontab'
print time() - t
def method2():
t = time()
for i in xrange(100000):
s = ''.join(['pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab','pythontab'])
print time() -t
method1()
method2()
Results:
0.641695976257
0.341440916061
Experiment 2:
# -*- coding: utf-8 -*-
from time import time
def method1():
t = time()
for i in xrange(100000):
s = 'pythontab'+'pythontab'+'pythontab'+'pythontab'
print time() - t
def method2():
t = time()
for i in xrange(100000):
s = ''.join(['pythontab','pythontab','pythontab','pythontab'])
print time() -t
method1()
method2()
Results:
0.0265691280365
0.0522091388702
The two experiments presented completely different results. The only difference between the two experiments was the number of string concatenations.
Conclusion: the low efficiency of the plus connection occurs when multiple string connections are made consecutively. If the number of connections is small, the efficiency of the plus connection is higher than that of the join connection