绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%97%a0%e6%b3%95' title='鏃犳硶'>鏃犳硶 娆℃暟 toc 浠g爜绀轰緥 濡備綍 瑙e喅 鎿嶄綔 ret timer
绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪
1. 绠楁硶
绠楁硶姒傝堪
绠楁硶鏄绠楁満澶勭悊淇℃伅鐨勬湰璐紝鍥犱负璁$畻鏈虹▼搴忔湰璐ㄤ笂鏄竴涓畻娉曟潵鍛婅瘔璁$畻鏈虹‘鍒囩殑姝ラ鏉ユ墽琛屼竴涓寚瀹氱殑浠诲姟銆備竴鑸湴锛屽綋绠楁硶鍦ㄥ鐞嗕俊鎭椂锛屼細浠庤緭鍏ヨ澶囨垨鏁版嵁鐨勫瓨鍌ㄥ湴鍧€璇诲彇鏁版嵁锛屾妸缁撴灉鍐欏叆杈撳嚭璁惧鎴栨煇涓瓨鍌ㄥ湴鍧€渚涗互鍚庡啀璋冪敤銆?/p>
绠楁硶鏄嫭绔嬪瓨鍦ㄧ殑涓€绉嶈В鍐抽棶棰樼殑鏂规硶鍜屾€濇兂銆?/strong>
瀵逛簬绠楁硶鑰岃█锛屽疄鐜扮殑璇█骞朵笉閲嶈锛岄噸瑕佺殑鏄€濇兂銆?/p>
绠楁硶鐨勪簲澶х壒鎬?/p>
- 杈撳叆: 绠楁硶鍏锋湁0涓垨澶氫釜杈撳叆
- 杈撳嚭: 绠楁硶鑷冲皯鏈?涓垨澶氫釜杈撳嚭
- 鏈夌┓鎬?/strong>: 绠楁硶鍦ㄦ湁闄愮殑姝ラ涔嬪悗浼氳嚜鍔ㄧ粨鏉熻€屼笉浼氭棤闄愬惊鐜紝骞朵笖姣忎竴涓楠ゅ彲浠ュ湪鍙帴鍙楃殑鏃堕棿鍐呭畬鎴?/li>
- 纭畾鎬?/strong>锛氱畻娉曚腑鐨勬瘡涓€姝ラ兘鏈夌‘瀹氱殑鍚箟锛屼笉浼氬嚭鐜颁簩涔夋€?/li>
- 鍙鎬?/strong>锛氱畻娉曠殑姣忎竴姝ラ兘鏄彲琛岀殑锛屼篃灏辨槸璇存瘡涓€姝ラ兘鑳藉鎵ц鏈夐檺鐨勬鏁板畬鎴?/li>
- 纭畾鎬?/strong>锛氱畻娉曚腑鐨勬瘡涓€姝ラ兘鏈夌‘瀹氱殑鍚箟锛屼笉浼氬嚭鐜颁簩涔夋€?/li>
3. 鏃堕棿澶嶆潅搴?/h3>
璇勫垽瑙勫垯锛氶噺鍖栫畻娉曟墽琛岀殑鎿嶄綔/鎵ц姝ラ鐨勬暟閲?/p>
琛ㄧ幇褰㈠紡锛氬ぇO璁版硶
- 鏈€閲嶈鐨勯」锛氭椂闂村鏉傚害琛ㄨ揪寮忎腑鏈€鏈夋剰涔夌殑椤?/li>
- O(鏈€閲嶈鐨勯」):O(n),O(n**2)
甯歌鐨勬椂闂村鏉傚害锛?/p>
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)
浠g爜绀轰緥锛氳绠椾笅鍒楃畻娉曠殑鏃堕棿澶嶆潅搴?/p>
a=5
b=6
c=10
for i in range(n):
for j in range(n):
x = i * i
y = j * j
z = i * j
for k in range(n):
w = a*k + 45
v = b*b
d = 33
# 鏃堕棿澶嶆潅搴︼細4+3n**2+2n==>n**2
4. 鏁版嵁缁撴瀯
- 姒傚康锛氬浜庢暟鎹紙鍩烘湰绫诲瀷鐨勬暟鎹紙int,float,char锛夛級鐨勭粍缁囨柟寮忓氨琚О浣滀负鏁版嵁缁撴瀯銆傛暟鎹粨鏋勮В鍐崇殑灏辨槸涓€缁勬暟鎹浣曡繘琛屼繚瀛橈紝淇濆瓨褰㈠紡鏄€庢牱鐨勩€?/li>
5. python鏁版嵁缁撴瀯鎬ц兘鍒嗘瀽
timeit妯″潡锛氳妯″潡鍙互鐢ㄦ潵娴嬭瘯涓€娈祊ython浠g爜鐨勬墽琛岄€熷害/鏃堕暱銆?/p>
Timer绫伙細璇ョ被鏄痶imeit妯″潡涓笓闂ㄧ敤浜庢祴閲弍ython浠g爜鐨勬墽琛岄€熷害/鏃堕暱鐨勩€傚師鍨嬩负锛歝lass timeit.Timer(stmt=鈥榩ass鈥?setup=鈥榩ass鈥?銆?/p>
- stmt鍙傛暟锛氳〃绀哄嵆灏嗚繘琛屾祴璇曠殑浠g爜鍧楄鍙ャ€?/li>
- setup锛氳繍琛屼唬鐮佸潡璇彞鏃舵墍闇€瑕佺殑璁剧疆銆?/li>
- timeit鍑芥暟锛歵imeit.Timer.timeit(number=100000),璇ュ嚱鏁拌繑鍥炰唬鐮佸潡璇彞鎵цnumber娆$殑骞冲潎鑰楁椂銆?/li>
浠g爜绀轰緥锛?/p>
import timeit
def test01():
alist = []
for i in range(1,1001):
alist += [i]
return alist
def test02():
alist = []
for i in range(1,1001):
alist.append(i)
return alist
def test03():
alist = list(range(1,1001))
return alist
def test04():
alist = [i for i in range(1,1001)]
return alist
if __name__ == '__main__':
#stmt:寰呮墽琛岀殑浠g爜鍧? #setup:
timer = timeit.Timer(stmt='test01()',setup='from __main__ import test01')
print(timer.timeit(5000))
timer1 = timeit.Timer(stmt='test02()',setup='from __main__ import test02')
print(timer1.timeit(5000))
timer2 = timeit.Timer(stmt='test03()',setup='from __main__ import test03')
print(timer2.timeit(5000))
timer3 = timeit.Timer(stmt='test04()',setup='from __main__ import test04')
print(timer3.timeit(5000))
"""
0.41899349999999913
0.4362713999999954
0.0754374999999925
0.2028786999999994
"""
6. 鎬荤粨
- 浣跨敤涓嶅悓鐨勫舰寮忕粍缁囨暟鎹紝鍦ㄥ熀浜庢煡璇㈡椂鐨勬椂闂村鏉傚害鏄笉涓€鏍风殑銆傚洜姝よ涓虹畻娉曟槸涓轰簡瑙e喅瀹為檯闂鑰岃璁$殑锛屾暟鎹粨鏋勬槸绠楁硶闇€瑕佸鐞嗛棶棰樼殑杞戒綋銆?/li>
璇勫垽瑙勫垯锛氶噺鍖栫畻娉曟墽琛岀殑鎿嶄綔/鎵ц姝ラ鐨勬暟閲?/p>
琛ㄧ幇褰㈠紡锛氬ぇO璁版硶
- 鏈€閲嶈鐨勯」锛氭椂闂村鏉傚害琛ㄨ揪寮忎腑鏈€鏈夋剰涔夌殑椤?/li>
- O(鏈€閲嶈鐨勯」):O(n),O(n**2)
甯歌鐨勬椂闂村鏉傚害锛?/p>
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)
浠g爜绀轰緥锛氳绠椾笅鍒楃畻娉曠殑鏃堕棿澶嶆潅搴?/p>
a=5
b=6
c=10
for i in range(n):
for j in range(n):
x = i * i
y = j * j
z = i * j
for k in range(n):
w = a*k + 45
v = b*b
d = 33
# 鏃堕棿澶嶆潅搴︼細4+3n**2+2n==>n**2
timeit妯″潡锛氳妯″潡鍙互鐢ㄦ潵娴嬭瘯涓€娈祊ython浠g爜鐨勬墽琛岄€熷害/鏃堕暱銆?/p>
Timer绫伙細璇ョ被鏄痶imeit妯″潡涓笓闂ㄧ敤浜庢祴閲弍ython浠g爜鐨勬墽琛岄€熷害/鏃堕暱鐨勩€傚師鍨嬩负锛歝lass timeit.Timer(stmt=鈥榩ass鈥?setup=鈥榩ass鈥?銆?/p>
- stmt鍙傛暟锛氳〃绀哄嵆灏嗚繘琛屾祴璇曠殑浠g爜鍧楄鍙ャ€?/li>
- setup锛氳繍琛屼唬鐮佸潡璇彞鏃舵墍闇€瑕佺殑璁剧疆銆?/li>
- timeit鍑芥暟锛歵imeit.Timer.timeit(number=100000),璇ュ嚱鏁拌繑鍥炰唬鐮佸潡璇彞鎵цnumber娆$殑骞冲潎鑰楁椂銆?/li>
浠g爜绀轰緥锛?/p>
import timeit
def test01():
alist = []
for i in range(1,1001):
alist += [i]
return alist
def test02():
alist = []
for i in range(1,1001):
alist.append(i)
return alist
def test03():
alist = list(range(1,1001))
return alist
def test04():
alist = [i for i in range(1,1001)]
return alist
if __name__ == '__main__':
#stmt:寰呮墽琛岀殑浠g爜鍧? #setup:
timer = timeit.Timer(stmt='test01()',setup='from __main__ import test01')
print(timer.timeit(5000))
timer1 = timeit.Timer(stmt='test02()',setup='from __main__ import test02')
print(timer1.timeit(5000))
timer2 = timeit.Timer(stmt='test03()',setup='from __main__ import test03')
print(timer2.timeit(5000))
timer3 = timeit.Timer(stmt='test04()',setup='from __main__ import test04')
print(timer3.timeit(5000))
"""
0.41899349999999913
0.4362713999999954
0.0754374999999925
0.2028786999999994
"""
以上是关于绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪的主要内容,如果未能解决你的问题,请参考以下文章