3 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
來自:高效字符串連接
方法1:
def method1(): out_str = '' for num in xrange(loop_count): out_str += 'num' return out_str
方法4:
def method4(): str_list = [] for num in xrange(loop_count): str_list.append('num') return ''.join(str_list)
現(xiàn)在我意識到它們并不具有嚴(yán)格的代表性,并且第四種方法在迭代并加入每個(gè)項(xiàng)目之前附加到列表中,但這是一個(gè)公平的指示。
字符串連接比串聯(lián)快得多。
為什么?字符串是不可變的,不能在適當(dāng)?shù)奈恢眠M(jìn)行更改。要改變一個(gè),需要?jiǎng)?chuàng)建一個(gè)新的表示(兩者的串聯(lián))。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
我的原始代碼是錯(cuò)誤的,似乎+
連接通常更快(特別是在較新的硬件上使用較新版本的Python)
時(shí)間如下:
Iterations: 1,000,000
Windows 7,Core i7上的Python 3.3
String of len: 1 took: 0.5710 0.2880 secondsString of len: 4 took: 0.9480 0.5830 secondsString of len: 6 took: 1.2770 0.8130 secondsString of len: 12 took: 2.0610 1.5930 secondsString of len: 80 took: 10.5140 37.8590 secondsString of len: 222 took: 27.3400 134.7440 secondsString of len: 443 took: 52.9640 170.6440 seconds
Windows 7,Core i7上的Python 2.7
String of len: 1 took: 0.7190 0.4960 secondsString of len: 4 took: 1.0660 0.6920 secondsString of len: 6 took: 1.3300 0.8560 secondsString of len: 12 took: 1.9980 1.5330 secondsString of len: 80 took: 9.0520 25.7190 secondsString of len: 222 took: 23.1620 71.3620 secondsString of len: 443 took: 44.3620 117.1510 seconds
在Linux Mint上,Python 2.7,一些較慢的處理器
String of len: 1 took: 1.8840 1.2990 secondsString of len: 4 took: 2.8394 1.9663 secondsString of len: 6 took: 3.5177 2.4162 secondsString of len: 12 took: 5.5456 4.1695 secondsString of len: 80 took: 27.8813 19.2180 secondsString of len: 222 took: 69.5679 55.7790 secondsString of len: 443 took: 135.6101 153.8212 seconds
以下是代碼:
from __future__ import print_functionimport timedef strcat(string): newstr = '' for char in string: newstr += char return newstrdef listcat(string): chars = [] for char in string: chars.append(char) return ''.join(chars)def test(fn, times, *args): start = time.time() for x in range(times): fn(*args) return "{:>10.4f}".format(time.time() - start)def testall(): strings = ['a', 'long', 'longer', 'a bit longer', '''adjkrsn widn fskejwoskemwkoskdfisdfasdfjiz oijewf sdkjjka dsf sdk siasjk dfwijs''', '''this is a really long string that's so long it had to be triple quoted and contains lots of superflous characters for kicks and gigles @!#(*_#)(*$(*!#@&)(*E\xc4\x32\xff\x92\x23\xDF\xDFk^%#$!)%#^(*#''', '''I needed another long string but this one won't have any new lines or crazy characters in it, I'm just going to type normal characters that I would usually write blah blah blah blah this is some more text hey cool what's crazy is that it looks that the str += is really close to the O(n^2) worst case performance, but it looks more like the other method increases in a perhaps linear scale? I don't know but I think this is enough text I hope.'''] for string in strings: print("String of len:", len(string), "took:", test(listcat, 1000000, string), test(strcat, 1000000, string), "seconds")testall()

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
現(xiàn)有的答案寫得非常好,研究得很好,但這是Python 3.6時(shí)代的另一個(gè)答案,因?yàn)楝F(xiàn)在我們有文字字符串插值(AKA,f
-strings):
>>> import timeit>>> timeit.timeit('f\'{"a"}{"b"}{"c"}\'', number=1000000)0.14618930302094668>>> timeit.timeit('"".join(["a", "b", "c"])', number=1000000)0.23334730707574636>>> timeit.timeit('a = "a"; a += "b"; a += "c"', number=1000000)0.14985873899422586
使用CPython 3.6.5在2012年Retina MacBook Pro上進(jìn)行測試,其中Intel Core i7為2.3 GHz。
這絕不是任何正式的基準(zhǔn)測試,但看起來使用f
-strings與使用+=
連接一樣高效; 當(dāng)然,任何改進(jìn)的指標(biāo)或建議都是受歡迎的。
添加回答
舉報(bào)