绠楁硶鏁版嵁缁撴瀯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>

    1. 杈撳叆: 绠楁硶鍏锋湁0涓垨澶氫釜杈撳叆
    2. 杈撳嚭: 绠楁硶鑷冲皯鏈?涓垨澶氫釜杈撳嚭
    3. 鏈夌┓鎬?/strong>: 绠楁硶鍦ㄦ湁闄愮殑姝ラ涔嬪悗浼氳嚜鍔ㄧ粨鏉熻€屼笉浼氭棤闄愬惊鐜紝骞朵笖姣忎竴涓楠ゅ彲浠ュ湪鍙帴鍙楃殑鏃堕棿鍐呭畬鎴?/li>
    4. 纭畾鎬?/strong>锛氱畻娉曚腑鐨勬瘡涓€姝ラ兘鏈夌‘瀹氱殑鍚箟锛屼笉浼氬嚭鐜颁簩涔夋€?/li>
    5. 鍙鎬?/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>

以上是关于绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪的主要内容,如果未能解决你的问题,请参考以下文章

python鍐呯疆鏁版嵁缁撴瀯

鏁版嵁缁撴瀯-C璇█瀹炵幇-閾炬爤

鏁版嵁缁撴瀯鐭ヨ瘑妗嗘灦

鏁版嵁缁撴瀯锛堜竴锛夌嚎鎬ц〃寰幆閾捐〃鐩稿叧琛ュ厖

鏁版嵁缁撴瀯瀹為獙涔嬫爤涓庨槦鍒楀崄涓€锛歳efresh鐨勫仠杞﹀満

鏁版嵁缁撴瀯瀹為獙涔嬮摼琛ㄥ洓锛氭湁搴忛摼琛ㄧ殑褰掑苟

(c)2006-2024 SYSTEM All Rights Reserved IT常识