{"id":1160326,"date":"2025-01-13T19:03:36","date_gmt":"2025-01-13T11:03:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160326.html"},"modified":"2025-01-13T19:03:39","modified_gmt":"2025-01-13T11:03:39","slug":"python%e8%a3%85%e9%a5%b0%e5%99%a8%e5%a6%82%e4%bd%95%e6%b5%8b%e8%af%95%e6%95%88%e7%8e%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160326.html","title":{"rendered":"python\u88c5\u9970\u5668\u5982\u4f55\u6d4b\u8bd5\u6548\u7387"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201819\/2c1e7dc1-e83a-41f7-b4f5-47d9d1ee09cc.webp\" alt=\"python\u88c5\u9970\u5668\u5982\u4f55\u6d4b\u8bd5\u6548\u7387\" \/><\/p>\n<p><p> <strong>Python\u88c5\u9970\u5668\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528time\u3001timeit\u6a21\u5757\u6765\u6d4b\u8bd5\u6548\u7387\u3001\u901a\u8fc7\u7f16\u5199\u7b80\u5355\u7684\u8ba1\u65f6\u88c5\u9970\u5668\u6765\u6d4b\u8bd5\u6548\u7387\u3001\u901a\u8fc7profile\u6a21\u5757\u6765\u6d4b\u8bd5\u6548\u7387<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u88c5\u9970\u5668\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u7684\u6027\u80fd\u8868\u73b0\uff0c\u4ece\u800c\u4f18\u5316\u4ee3\u7801\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u901a\u8fc7time\u6a21\u5757\u6d4b\u8bd5\u6548\u7387<\/strong>\uff1a<\/p>\n<p>time\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u3002\u4f7f\u7528time\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u8ba1\u65f6\u88c5\u9970\u5668\u6765\u6d4b\u8bd5\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def time_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;Execution time: {end_time - start_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@time_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    time.sleep(2)  # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u6548\u7387<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>time_decorator<\/code>\u7684\u88c5\u9970\u5668\uff0c\u5b83\u4f1a\u6d4b\u91cf\u88ab\u88c5\u9970\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u51fa\u6765\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6d4b\u8bd5\u4efb\u610f\u51fd\u6570\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u88c5\u9970\u5668\u6548\u7387\u6d4b\u8bd5\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u5177\u4f53\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u901a\u8fc7time\u6a21\u5757\u6d4b\u8bd5\u6548\u7387<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528time\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>time\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528time\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u7247\u6bb5<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = time.time()<\/p>\n<p>execution_time = end_time - start_time<\/p>\n<p>print(f&quot;Execution time: {execution_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>time.time()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u5e76\u8ba1\u7b97\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u524d\u540e\u7684\u65f6\u95f4\u5dee\uff0c\u4ece\u800c\u5f97\u5230\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u8ba1\u65f6\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u6d4b\u91cf\u6267\u884c\u65f6\u95f4\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u88c5\u9970\u5668\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u6548\u7387\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def time_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;Execution time: {end_time - start_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@time_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    time.sleep(2)  # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u6548\u7387<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>time_decorator<\/code>\u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e<code>sample_function<\/code>\u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528<code>sample_function<\/code>\u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u901a\u8fc7timeit\u6a21\u5757\u6d4b\u8bd5\u6548\u7387<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528timeit\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>timeit\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u3002\u4e0etime\u6a21\u5757\u76f8\u6bd4\uff0ctimeit\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u7cbe\u786e\u548c\u65b9\u4fbf\u7684\u6d4b\u91cf\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528timeit\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u7247\u6bb5<\/strong><\/h2>\n<p>code_to_test = &quot;&quot;&quot;<\/p>\n<p>a = [i for i in range(1000)]<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>execution_time = timeit.timeit(code_to_test, number=1000)<\/p>\n<p>print(f&quot;Execution time: {execution_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>timeit.timeit<\/code>\u51fd\u6570\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002<code>timeit.timeit<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u6b21\u6570\u3002\u51fd\u6570\u8fd4\u56de\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u6307\u5b9a\u6b21\u6570\u7684\u603b\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u8ba1\u65f6\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06timeit\u6a21\u5757\u7684\u6d4b\u91cf\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u88c5\u9970\u5668\u4e2d\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u6548\u7387\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528timeit\u6a21\u5757\u7684\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>def timeit_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        code_to_test = f&quot;&quot;&quot;<\/p>\n<p>result = func(*args, kwargs)<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>        execution_time = timeit.timeit(code_to_test, globals=globals(), number=1000)<\/p>\n<p>        print(f&quot;Execution time: {execution_time} seconds&quot;)<\/p>\n<p>        return func(*args, kwargs)<\/p>\n<p>    return wrapper<\/p>\n<p>@timeit_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u6548\u7387<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>timeit_decorator<\/code>\u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e<code>sample_function<\/code>\u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528<code>sample_function<\/code>\u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7profile\u6a21\u5757\u6d4b\u8bd5\u6548\u7387<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528profile\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>profile\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7528\u4e8e\u6027\u80fd\u5206\u6790\u7684\u5de5\u5177\u3002\u901a\u8fc7profile\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8be6\u7ec6\u4e86\u89e3\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\u548c\u8c03\u7528\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528profile\u6a21\u5757\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<p>cProfile.run(&#39;sample_function()&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>cProfile.run<\/code>\u51fd\u6570\u6d4b\u91cf<code>sample_function<\/code>\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790\u62a5\u544a\uff0c\u5305\u62ec\u51fd\u6570\u7684\u8c03\u7528\u6b21\u6570\u3001\u603b\u6267\u884c\u65f6\u95f4\u3001\u6bcf\u6b21\u8c03\u7528\u7684\u5e73\u5747\u6267\u884c\u65f6\u95f4\u7b49\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u8ba1\u65f6\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06profile\u6a21\u5757\u7684\u6d4b\u91cf\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u88c5\u9970\u5668\u4e2d\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u6548\u7387\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528profile\u6a21\u5757\u7684\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>import pstats<\/p>\n<p>import io<\/p>\n<p>def profile_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        pr = cProfile.Profile()<\/p>\n<p>        pr.enable()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        pr.disable()<\/p>\n<p>        s = io.StringIO()<\/p>\n<p>        sortby = &#39;cumulative&#39;<\/p>\n<p>        ps = pstats.Stats(pr, stream=s).sort_stats(sortby)<\/p>\n<p>        ps.print_stats()<\/p>\n<p>        print(s.getvalue())<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@profile_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u6548\u7387<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>profile_decorator<\/code>\u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e<code>sample_function<\/code>\u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528<code>sample_function<\/code>\u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u901a\u8fc7 memory_profiler \u6d4b\u8bd5\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5 memory_profiler<\/h3>\n<\/p>\n<p><p>memory_profiler \u662f\u4e00\u4e2a\u7528\u4e8e\u76d1\u6d4b Python \u7a0b\u5e8f\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u6a21\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88c5\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install memory_profiler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u5185\u5b58\u76d1\u6d4b\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 memory_profiler \u6a21\u5757\u7f16\u5199\u4e00\u4e2a\u5185\u5b58\u76d1\u6d4b\u88c5\u9970\u5668\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5185\u5b58\u76d1\u6d4b\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from memory_profiler import profile<\/p>\n<p>@profile<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>profile<\/code> \u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e <code>sample_function<\/code> \u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528 <code>sample_function<\/code> \u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7ed3\u5408 time \u548c memory_profiler<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u65f6\u95f4\u548c\u5185\u5b58\u76d1\u6d4b\u7ed3\u5408\u8d77\u6765\uff0c\u7f16\u5199\u4e00\u4e2a\u540c\u65f6\u76d1\u6d4b\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u7684\u88c5\u9970\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u4e86 time \u548c memory_profiler \u7684\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from memory_profiler import memory_usage<\/p>\n<p>def time_memory_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        mem_usage_before = memory_usage()[0]<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        mem_usage_after = memory_usage()[0]<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;Execution time: {end_time - start_time} seconds&quot;)<\/p>\n<p>        print(f&quot;Memory usage: {mem_usage_after - mem_usage_before} MiB&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@time_memory_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>time_memory_decorator<\/code> \u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e <code>sample_function<\/code> \u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528 <code>sample_function<\/code> \u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u540c\u65f6\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u901a\u8fc7 line_profiler \u6d4b\u8bd5\u9010\u884c\u6027\u80fd<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5 line_profiler<\/h3>\n<\/p>\n<p><p>line_profiler \u662f\u4e00\u4e2a\u7528\u4e8e\u9010\u884c\u6027\u80fd\u5206\u6790\u7684\u6a21\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88c5\u8fd9\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install line_profiler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u9010\u884c\u6027\u80fd\u76d1\u6d4b\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 line_profiler \u6a21\u5757\u7f16\u5199\u4e00\u4e2a\u9010\u884c\u6027\u80fd\u76d1\u6d4b\u88c5\u9970\u5668\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u9010\u884c\u6027\u80fd\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9010\u884c\u6027\u80fd\u76d1\u6d4b\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from line_profiler import LineProfiler<\/p>\n<p>def line_profile_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        profiler = LineProfiler()<\/p>\n<p>        profiler.add_function(func)<\/p>\n<p>        profiler.enable_by_count()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        profiler.disable_by_count()<\/p>\n<p>        profiler.print_stats()<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@line_profile_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u9010\u884c\u6027\u80fd<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>line_profile_decorator<\/code> \u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e <code>sample_function<\/code> \u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528 <code>sample_function<\/code> \u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u9010\u884c\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u901a\u8fc7 perf_counter \u6d4b\u8bd5\u9ad8\u7cbe\u5ea6\u65f6\u95f4<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528 perf_counter \u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>perf_counter \u662f time \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u5668\u51fd\u6570\uff0c\u9002\u5408\u7528\u4e8e\u6d4b\u91cf\u77ed\u65f6\u95f4\u95f4\u9694\u7684\u6267\u884c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 perf_counter \u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.perf_counter()<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u7247\u6bb5<\/strong><\/h2>\n<p>time.sleep(2)  # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>end_time = time.perf_counter()<\/p>\n<p>execution_time = end_time - start_time<\/p>\n<p>print(f&quot;Execution time: {execution_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>time.perf_counter<\/code> \u51fd\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u9ad8\u7cbe\u5ea6\u65f6\u95f4\u6233\uff0c\u5e76\u8ba1\u7b97\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u524d\u540e\u7684\u65f6\u95f4\u5dee\uff0c\u4ece\u800c\u5f97\u5230\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u6d4b\u91cf\u6267\u884c\u65f6\u95f4\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u88c5\u9970\u5668\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u6548\u7387\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def perf_counter_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.perf_counter()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.perf_counter()<\/p>\n<p>        print(f&quot;Execution time: {end_time - start_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@perf_counter_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    time.sleep(2)  # \u6a21\u62df\u4e00\u4e2a\u8017\u65f6\u64cd\u4f5c<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u9ad8\u7cbe\u5ea6\u65f6\u95f4<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>perf_counter_decorator<\/code> \u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e <code>sample_function<\/code> \u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528 <code>sample_function<\/code> \u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u9ad8\u7cbe\u5ea6\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u901a\u8fc7 tracemalloc \u6d4b\u8bd5\u5185\u5b58\u5206\u914d<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528 tracemalloc \u6d4b\u91cf\u5185\u5b58\u5206\u914d\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>tracemalloc \u662f Python 3.4 \u53ca\u4ee5\u4e0a\u7248\u672c\u63d0\u4f9b\u7684\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u8ddf\u8e2a\u6a21\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 tracemalloc \u6765\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u5185\u5b58\u5206\u914d\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 tracemalloc \u6d4b\u91cf\u5185\u5b58\u5206\u914d\u60c5\u51b5\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tracemalloc<\/p>\n<p>tracemalloc.start()<\/p>\n<h2><strong>\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\u7247\u6bb5<\/strong><\/h2>\n<p>a = [i for i in range(1000)]<\/p>\n<p>snapshot = tracemalloc.take_snapshot()<\/p>\n<p>top_stats = snapshot.statistics(&#39;lineno&#39;)<\/p>\n<p>for stat in top_stats[:10]:<\/p>\n<p>    print(stat)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>tracemalloc.start()<\/code> \u542f\u52a8\u5185\u5b58\u5206\u914d\u8ddf\u8e2a\uff0c\u5e76\u4f7f\u7528 <code>tracemalloc.take_snapshot()<\/code> \u83b7\u53d6\u5185\u5b58\u5206\u914d\u7684\u5feb\u7167\u3002\u7136\u540e\uff0c\u6211\u4eec\u6253\u5370\u5185\u5b58\u5206\u914d\u6700\u591a\u7684\u524d 10 \u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u5185\u5b58\u5206\u914d\u76d1\u6d4b\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u6d4b\u91cf\u5185\u5b58\u5206\u914d\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u88c5\u9970\u5668\u4e2d\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u5185\u5b58\u5206\u914d\u60c5\u51b5\u7684\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u76d1\u6d4b\u88c5\u9970\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tracemalloc<\/p>\n<p>def tracemalloc_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        tracemalloc.start()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        snapshot = tracemalloc.take_snapshot()<\/p>\n<p>        top_stats = snapshot.statistics(&#39;lineno&#39;)<\/p>\n<p>        for stat in top_stats[:10]:<\/p>\n<p>            print(stat)<\/p>\n<p>        tracemalloc.stop()<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@tracemalloc_decorator<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<h2><strong>\u8c03\u7528\u88c5\u9970\u5668\u6d4b\u8bd5\u5185\u5b58\u5206\u914d\u60c5\u51b5<\/strong><\/h2>\n<p>sample_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>tracemalloc_decorator<\/code> \u7684\u88c5\u9970\u5668\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e <code>sample_function<\/code> \u51fd\u6570\u3002\u6bcf\u6b21\u8c03\u7528 <code>sample_function<\/code> \u65f6\uff0c\u88c5\u9970\u5668\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u5185\u5b58\u5206\u914d\u60c5\u51b5\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u901a\u8fc7 pytest-benchmark \u6d4b\u8bd5\u57fa\u51c6\u6027\u80fd<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5 pytest-benchmark<\/h3>\n<\/p>\n<p><p>pytest-benchmark \u662f\u4e00\u4e2a\u7528\u4e8e\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5\u7684 pytest \u63d2\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88c5\u8fd9\u4e2a\u63d2\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytest-benchmark<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7f16\u5199\u57fa\u51c6\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pytest-benchmark \u7f16\u5199\u57fa\u51c6\u6d4b\u8bd5\uff0c\u4ee5\u4fbf\u5bf9\u591a\u4e2a\u51fd\u6570\u8fdb\u884c\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytest<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<p>def test_sample_function(benchmark):<\/p>\n<p>    result = benchmark(sample_function)<\/p>\n<p>    assert result == &quot;Function completed&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>test_sample_function<\/code> \u7684\u57fa\u51c6\u6d4b\u8bd5\u51fd\u6570\uff0c\u5e76\u4f7f\u7528 <code>benchmark<\/code> \u5bf9 <code>sample_function<\/code> \u8fdb\u884c\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5\u3002\u6bcf\u6b21\u8fd0\u884c\u6d4b\u8bd5\u65f6\uff0c\u63d2\u4ef6\u90fd\u4f1a\u6d4b\u91cf\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pytest \u547d\u4ee4\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\uff0c\u5e76\u67e5\u770b\u6d4b\u8bd5\u62a5\u544a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pytest --benchmark-only<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0cpytest \u4f1a\u6267\u884c\u57fa\u51c6\u6d4b\u8bd5\u5e76\u6253\u5370\u8be6\u7ec6\u7684\u6027\u80fd\u62a5\u544a\uff0c\u5305\u62ec\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3001\u6807\u51c6\u5dee\u3001\u6700\u5c0f\u65f6\u95f4\u3001\u6700\u5927\u65f6\u95f4\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u901a\u8fc7 cProfile \u548c pstats \u7ed3\u5408\u4f7f\u7528<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528 cProfile \u548c pstats \u7ed3\u5408\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06 cProfile \u548c pstats \u6a21\u5757\u7ed3\u5408\u8d77\u6765\uff0c\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 cProfile \u548c pstats \u7ed3\u5408\u6d4b\u91cf\u4ee3\u7801\u7247\u6bb5\u6267\u884c\u65f6\u95f4\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cProfile<\/p>\n<p>import pstats<\/p>\n<p>import io<\/p>\n<p>def sample_function():<\/p>\n<p>    a = [i for i in range(1000)]<\/p>\n<p>    return &quot;Function completed&quot;<\/p>\n<p>pr = cProfile.Profile()<\/p>\n<p>pr.enable()<\/p>\n<p>sample_function()<\/p>\n<p>pr.disable()<\/p>\n<p>s = io.StringIO()<\/p>\n<p>sortby = &#39;cumulative&#39;<\/p>\n<p>ps = pstats.Stats(pr, stream=s).sort_stats(sortby)<\/p>\n<p>ps.print_stats()<\/p>\n<p>print(s.getvalue())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>cProfile.Profile<\/code> \u542f\u52a8\u6027\u80fd\u5206\u6790\uff0c\u5e76\u4f7f\u7528 <code>pstats.Stats<\/code> \u6253\u5370\u8be6\u7ec6\u7684\u6027\u80fd\u5206\u6790<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8bc4\u4f30Python\u88c5\u9970\u5668\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u8981\u8bc4\u4f30Python\u88c5\u9970\u5668\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u88c5\u9970\u5668\u5e94\u7528\u524d\u540e\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u5728\u88c5\u9970\u5668\u5185\u90e8\u8bb0\u5f55\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u4e86\u89e3\u88c5\u9970\u5668\u5bf9\u51fd\u6570\u6267\u884c\u901f\u5ea6\u7684\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u53ef\u4ee5\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u6027\u80fd\u6d4b\u8bd5\uff0c\u5c24\u5176\u662f\u5f53\u51fd\u6570\u6267\u884c\u65f6\u95f4\u8f83\u77ed\u65f6\uff0c<code>timeit<\/code>\u80fd\u591f\u63d0\u4f9b\u66f4\u53ef\u9760\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u88c5\u9970\u5668\u65f6\u6709\u54ea\u4e9b\u6027\u80fd\u9690\u60a3\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u88c5\u9970\u5668\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u6027\u80fd\u95ee\u9898\uff0c\u4f8b\u5982\u8fc7\u591a\u7684\u88c5\u9970\u5668\u5d4c\u5957\u4f1a\u5bfc\u81f4\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u589e\u52a0\u3002\u6b64\u5916\uff0c\u5982\u679c\u88c5\u9970\u5668\u6267\u884c\u4e86\u590d\u6742\u7684\u8ba1\u7b97\u6216\u6d89\u53caI\/O\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u663e\u8457\u5f71\u54cd\u51fd\u6570\u7684\u6574\u4f53\u6027\u80fd\u3002\u5efa\u8bae\u5728\u8bbe\u8ba1\u88c5\u9970\u5668\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u5176\u903b\u8f91\u7b80\u5355\uff0c\u4ee5\u51cf\u5c11\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u88c5\u9970\u5668\u4ee5\u63d0\u9ad8\u6548\u7387\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u88c5\u9970\u5668\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u6cd5\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u88c5\u9970\u5668\u7684\u903b\u8f91\u5c3d\u91cf\u7b80\u5316\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u64cd\u4f5c\u3002\u5176\u6b21\uff0c\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u6765\u5b58\u50a8\u51fd\u6570\u7684\u7ed3\u679c\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u7684\u5f00\u9500\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>functools.wraps<\/code>\u6765\u4fdd\u7559\u88ab\u88c5\u9970\u51fd\u6570\u7684\u5143\u6570\u636e\uff0c\u907f\u514d\u56e0\u88c5\u9970\u5668\u5bfc\u81f4\u7684\u6027\u80fd\u635f\u5931\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u88c5\u9970\u5668\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u88c5\u9970\u5668\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528time\u3001timeit\u6a21\u5757\u6765\u6d4b\u8bd5\u6548\u7387\u3001\u901a\u8fc7\u7f16\u5199\u7b80\u5355\u7684\u8ba1\u65f6\u88c5\u9970\u5668\u6765\u6d4b\u8bd5\u6548\u7387\u3001\u901a\u8fc7p [&hellip;]","protected":false},"author":3,"featured_media":1160338,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160326"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1160326"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160326\/revisions"}],"predecessor-version":[{"id":1160340,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160326\/revisions\/1160340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}