{"id":1950,"date":"2023-11-28T16:50:25","date_gmt":"2023-11-28T08:50:25","guid":{"rendered":"http:\/\/zhang.mba\/?p=1950"},"modified":"2023-11-28T16:52:57","modified_gmt":"2023-11-28T08:52:57","slug":"python-timeit-ce-chu-dai-ma-zhi-xing-shi-jianzhao","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/28\/16\/50\/25\/1950\/python-timeit-ce-chu-dai-ma-zhi-xing-shi-jianzhao\/python\/zhangzhiqi\/","title":{"rendered":"Python Timeit\uff1a\u6d4b\u51fa\u4ee3\u7801\u6267\u884c\u65f6\u95f4_\u627e\u51fa\u6027\u80fd\u74f6\u9888"},"content":{"rendered":"<p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u6027\u80fd\u95ee\u9898\u3002\u6709\u65f6\u5019\uff0c\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u80fd\u9700\u8981\u6267\u884c\u6570\u767e\u4e07\u6b21\uff0c\u800c\u4e14\u6bcf\u6b21\u6267\u884c\u90fd\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u9700\u8981\u4f7f\u7528Python Timeit\u6a21\u5757\u6765\u6d4b\u8bd5\u6211\u4eec\u7684\u4ee3\u7801\u6027\u80fd\uff0c\u4ee5\u4fbf\u627e\u51fa\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<p>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython Timeit\u6a21\u5757\u7684\u4f7f\u7528\u6559\u7a0b\uff0c\u8ba9\u4f60\u7684\u4ee3\u7801\u901f\u5ea6\u77ac\u95f4\u63d0\u5347100\u500d\uff01<\/p>\n<p>1. Python Timeit\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>Python Timeit\u662f\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u6d4b\u91cfPython\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u627e\u51fa\u4ee3\u7801\u4e2d\u7684\u6027\u80fd\u95ee\u9898\uff0c\u4ee5\u4fbf\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<p>2. Python Timeit\u7684\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p>\u4f7f\u7528Python Timeit\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5f15\u5165Timeit\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aTimer\u5bf9\u8c61\uff0c\u5c06\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9Timer\u5bf9\u8c61\u3002Timer\u5bf9\u8c61\u4f1a\u81ea\u52a8\u8fd0\u884c\u4ee3\u7801\u591a\u6b21\uff0c\u5e76\u8ba1\u7b97\u5e73\u5747\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python Timeit\u6765\u6d4b\u8bd5\u4e00\u4e2a\u7b80\u5355\u7684for\u5faa\u73af\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import timeit\ndef test():\n    for i in range(1000):\n        pass\nprint(timeit.timeit(test, number=1000))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2atest\u51fd\u6570\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u7b80\u5355\u7684for\u5faa\u73af\u3002\u6211\u4eec\u4f7f\u7528timeit.timeit\u51fd\u6570\u6765\u6d4b\u8bd5\u8fd9\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002timeit.timeit\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u548c\u8fd0\u884c\u6d4b\u8bd5\u7684\u6b21\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u8fd0\u884c\u6d4b\u8bd51000\u6b21\u3002<\/p>\n<p>3. Python Timeit\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7528\u6cd5\u4e4b\u5916\uff0cPython Timeit\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7528\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6d4b\u8bd5\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<p>3.1 \u6d4b\u8bd5\u4ee3\u7801\u5757<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u4e00\u4e9b\u4ee3\u7801\u5757\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Timer\u5bf9\u8c61\u7684timeit\u65b9\u6cd5\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import timeit\ncode = '''\nfor i in range(1000):\n    pass\n'''\nprint(timeit.timeit(stmt=code, number=1000))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2acode\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7b80\u5355\u7684for\u5faa\u73af\u3002\u6211\u4eec\u4f7f\u7528timeit.timeit\u51fd\u6570\u6765\u6d4b\u8bd5\u8fd9\u4e2a\u4ee3\u7801\u5757\u7684\u6267\u884c\u65f6\u95f4\u3002timeit.timeit\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u5757\u548c\u8fd0\u884c\u6d4b\u8bd5\u7684\u6b21\u6570\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u8fd0\u884c\u6d4b\u8bd51000\u6b21\u3002<\/p>\n<p>3.2 \u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Timer\u5bf9\u8c61\u7684setup\u53c2\u6570\u6765\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import timeit\ncode = '''\nfor i in range(n):\n    pass\n'''\nprint(timeit.timeit(stmt=code, setup='n=1000', number=1000))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528setup\u53c2\u6570\u6765\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cfn\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\u3002<\/p>\n<p>3.3 \u6d4b\u8bd5\u591a\u4e2a\u51fd\u6570<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u591a\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Timer\u5bf9\u8c61\u7684repeat\u65b9\u6cd5\u6765\u6d4b\u8bd5\u591a\u4e2a\u51fd\u6570\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import timeit\ndef test1():\n    for i in range(1000):\n        pass\ndef test2():\n    for i in range(10000):\n        pass\nprint(timeit.repeat(test1, number=1000, repeat=3))\nprint(timeit.repeat(test2, number=1000, repeat=3))\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570test1\u548ctest2\u3002\u6211\u4eec\u4f7f\u7528timeit.repeat\u51fd\u6570\u6765\u6d4b\u8bd5\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002timeit.repeat\u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u8981\u6d4b\u8bd5\u7684\u51fd\u6570\u3001\u8fd0\u884c\u6d4b\u8bd5\u7684\u6b21\u6570\u548c\u91cd\u590d\u6d4b\u8bd5\u7684\u6b21\u6570\u3002<\/p>\n<p>4. \u603b\u7ed3<\/p>\n<p>Python Timeit\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u627e\u51fa\u4ee3\u7801\u4e2d\u7684\u6027\u80fd\u95ee\u9898\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u901a\u8fc7\u4f7f\u7528Python Timeit\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6d4b\u8bd5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ee5\u4fbf\u627e\u51fa\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u3002Python Timeit\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u8981\u521b\u5efa\u4e00\u4e2aTimer\u5bf9\u8c61\uff0c\u5e76\u5c06\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u5373\u53ef\u3002\u9664\u4e86\u57fa\u672c\u7528\u6cd5\u4e4b\u5916\uff0cPython Timeit\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7528\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6d4b\u8bd5\u4ee3\u7801\u6027\u80fd\u3002\u8ba9\u6211\u4eec\u4e00\u8d77\u638c\u63e1Python Timeit\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8ba9\u6211\u4eec\u7684\u4ee3\u7801\u901f\u5ea6\u77ac\u95f4\u63d0\u5347100\u500d\u5427\uff01<\/p>\n<p>\u6765\u6e90&#8212;&#8212;Python\u5c0f\u8d34\u58eb<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u6027\u80fd\u95ee\u9898\u3002\u6709\u65f6\u5019\uff0c\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u80fd\u9700\u8981\u6267\u884c\u6570\u767e\u4e07\u6b21\uff0c\u800c\u4e14\u6bcf\u6b21\u6267\u884c\u90fd\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u9700\u8981\u4f7f\u7528Python Timeit\u6a21\u5757\u6765\u6d4b\u8bd5\u6211\u4eec\u7684\u4ee3\u7801\u6027\u80fd\uff0c\u4ee5\u4fbf\u627e\u51fa\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1950"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1950"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1950\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}