Three methods of python string connection and their efficiency, detailed description of the applicable scenarios of python string connection methods, generally include the following three:
Method 1: connect directly with the plus sign (+) operator
website = 'python' + 'tab' + '.com'
Method 2: join
listStr = ['python', 'tab', '.com'] website = ''.join(listStr)
Method 3: Replace
website = '%s%s%s' % ('python', 'tab', '.com')
Next, let's talk about the differences between the three methods.
Method 1 is simple and straightforward, but many people on the Internet say this method is inefficient.
In python, the efficiency of using + to connect strings is low because strings in python are unchangeable. when using + to connect two strings, a new string is generated, to generate a new string, you need to re-apply for memory. when many strings are added consecutively (a + B + c + d + e + f + ...), inefficiency is inevitable.
Method 2: The usage is slightly complicated, but the connection validity of multiple characters is high, and only one memory application will be performed. In addition, this method must be the first choice when the list character is connected.
Method 3: string formatting. this method is very common and is recommended.
The following uses experiments to illustrate the efficiency of string connection.
Comparison object: Plus sign connection VS join connection python version: python2.7 system environment: CentOS
Lab 1:
# -*- coding: utf-8 -*-from time import timedef 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() - tdef 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() -tmethod1()method2()
Result:
0.6416959762570.341440916061
Lab 2:
# -*- coding: utf-8 -*-from time import timedef method1(): t = time() for i in xrange(100000): s = 'pythontab'+'pythontab'+'pythontab'+'pythontab' print time() - tdef method2(): t = time() for i in xrange(100000): s = ''.join(['pythontab','pythontab','pythontab','pythontab']) print time() -tmethod1()method2()
Result:
0.02656912803650.0522091388702
The above two experiments show completely different results. The only difference between the two experiments is the number of string connections.
Conclusion: the low efficiency of the plus sign connection occurs when multiple strings are connected consecutively. if the number of connections is small, the efficiency of the plus sign connection is higher than that of the join connection.