Python字符串通過'+'和join函數(shù)拼接新字符串的性能測試比較
有一道Python面試題, 以下代碼有什么局限性,要如何修改
def strTest(num):
s = 'Hello'
for i in range(num):
s += 'x'
return s
上面的代碼其實可以看出:由于變量str是不變對象,每次遍歷,Python都會生成新的str對象來存儲新的字符串,所以num越大,創(chuàng)建的str對象就越多,內存消耗約大,速度越慢,性能越差。 如果要改變上面的問題,可以變字符串拼接為join聯(lián)合的方式,代碼如下:
def strTest2(num):
s = 'Hello'
l = list(s)
for i in range(num):
l.append('x')
return ''.join(l)
下面兩種不同處理方式,運行速度的比較:
>>> def strTest1(num):
... s = 'Hello'
... for i in range(num):
... s += 'x'
... return s
>>> def strTest2(num):
... s = 'Hello'
... l = list(s)
... for i in range(num):
... l.append(s)
... return ''.join(l)
>>>
>>> from timeit import timeit
# 運行10萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(100000)", setup="from __main__ import strTest1", number=1)
0.016680980406363233
>>> timeit("strTest2(100000)", setup="from __main__ import strTest2", number=1)
0.009688869110618725
# 運行100萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(1000000)", setup="from __main__ import strTest1", number=1)
0.14558920607187195
>>> timeit("strTest2(1000000)", setup="from __main__ import strTest2", number=1)
0.1335057276853462
# 運行1000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(10000000)", setup="from __main__ import strTest1", number=1)
5.9497953107860475
>>> timeit("strTest2(10000000)", setup="from __main__ import strTest2", number=1)
1.3268972136649921
# 運行2000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(20000000)", setup="from __main__ import strTest1", number=1)
21.661270140499056
>>> timeit("strTest2(20000000)", setup="from __main__ import strTest2", number=1)
2.6981786518920217
# 運行3000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(30000000)", setup="from __main__ import strTest1", number=1)
49.858089123966295
>>> timeit("strTest2(30000000)", setup="from __main__ import strTest2", number=1)
4.285787770209481
# 運行4000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(40000000)", setup="from __main__ import strTest1", number=1)
86.67876273457563
>>> timeit("strTest2(40000000)", setup="from __main__ import strTest2", number=1)
5.328653452047092
# 運行5000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(50000000)", setup="from __main__ import strTest1", number=1)
130.59138063819023
>>> timeit("strTest2(50000000)", setup="from __main__ import strTest2", number=1)
6.8375931077291625
# 運行6000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(60000000)", setup="from __main__ import strTest1", number=1)
188.28227241975003
>>> timeit("strTest2(60000000)", setup="from __main__ import strTest2", number=1)
8.080144489401846
# 運行7000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(70000000)", setup="from __main__ import strTest1", number=1)
256.54383904350277
>>> timeit("strTest2(70000000)", setup="from __main__ import strTest2", number=1)
9.387400816458012
# 運行8000萬級別數(shù)據(jù),運行速度比對
>>> timeit("strTest1(80000000)", setup="from __main__ import strTest1", number=1)
333.7185806572388
>>> timeit("strTest2(80000000)", setup="from __main__ import strTest2", number=1)
10.946627677462857
從上面的比對數(shù)據(jù)可以看出,當數(shù)據(jù)比較小的時候,兩者差別不大,當數(shù)據(jù)越大,兩者性能差距就越大。從而可以看出,字符串拼接的方式一旦碰到大數(shù)據(jù)處理的時候,性能是非常慢的。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
- Python中join()函數(shù)多種操作代碼實例
- 詳解python路徑拼接os.path.join()函數(shù)的用法
- python3 字符串/列表/元組(str/list/tuple)相互轉換方法及join()函數(shù)的使用
- Python中.join()和os.path.join()兩個函數(shù)的用法詳解
- Python常見字符串操作函數(shù)小結【split()、join()、strip()】
- Python中threading模塊join函數(shù)用法實例分析
- 詳解Python中的join()函數(shù)的用法
- python多線程編程中的join函數(shù)使用心得
- Python join()函數(shù)原理及使用方法
相關文章
python實現(xiàn)全排列代碼(回溯、深度優(yōu)先搜索)
今天小編就為大家分享一篇python實現(xiàn)全排列代碼(回溯、深度優(yōu)先搜索),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python使用matplotlib.pyplot?as?plt繪圖圖層優(yōu)先級問題
這篇文章主要介紹了Python使用matplotlib.pyplot?as?plt繪圖圖層優(yōu)先級問題.文章圍繞主題展開詳細內容需要的小伙伴可以參考一下2022-04-04
Python處理文件的方法(mimetypes和chardet)
這篇文章主要介紹了Python處理文件的方法(mimetypes和chardet),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
使用Python合并PDF文件并添加自定義目錄及頁腳的全過程
在處理文檔時,我們經(jīng)常遇到需要合并多個PDF文件并添加目錄及頁腳的情況,本文將介紹如何使用Python,特別是PyPDF2和reportlab庫來實現(xiàn)這一功能我們將通過一個實用的示例來演示整個過程,需要的朋友可以參考下2024-03-03

