{"id":1152694,"date":"2025-01-13T17:29:03","date_gmt":"2025-01-13T09:29:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152694.html"},"modified":"2025-01-13T17:29:05","modified_gmt":"2025-01-13T09:29:05","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e5%a4%84%e7%90%86%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152694.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u5904\u7406\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182618\/2ec774f2-307f-42cc-98b3-077ea14b0658.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982perf_counter\u3002<\/strong> <\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528time\u6a21\u5757<\/strong>\uff1a\u901a\u8fc7time\u6a21\u5757\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bb0\u5f55\u4e00\u4e2a\u64cd\u4f5c\u7684\u5f00\u59cb\u65f6\u95f4\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u7136\u540e\u8ba1\u7b97\u4e24\u8005\u4e4b\u95f4\u7684\u5dee\u503c\u6765\u5f97\u5230\u5904\u7406\u65f6\u95f4\u3002<strong>\u4f7f\u7528timeit\u6a21\u5757<\/strong>\uff1atimeit\u6a21\u5757\u4e13\u95e8\u7528\u6765\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff0c\u5c24\u5176\u662f\u9002\u7528\u4e8e\u5c0f\u6bb5\u4ee3\u7801\u7684\u6027\u80fd\u6d4b\u8bd5\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884c\u591a\u6b21\u6d4b\u8bd5\u5e76\u8fd4\u56de\u5e73\u5747\u65f6\u95f4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528time\u6a21\u5757\u6765\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528time\u6a21\u5757\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/h3>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165time\u6a21\u5757\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u524d\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u540e\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u3002<\/li>\n<li>\u8ba1\u7b97\u7ed3\u675f\u65f6\u95f4\u548c\u5f00\u59cb\u65f6\u95f4\u7684\u5dee\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528datetime\u6a21\u5757\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/h3>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165datetime\u6a21\u5757\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u524d\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u540e\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u3002<\/li>\n<li>\u8ba1\u7b97\u7ed3\u675f\u65f6\u95f4\u548c\u5f00\u59cb\u65f6\u95f4\u7684\u5dee\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = datetime.now()<\/p>\n<h2><strong>\u6267\u884c\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = datetime.now()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Processing time: {processing_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528timeit\u6a21\u5757\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/h3>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165timeit\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528timeit.timeit()\u51fd\u6570\u6267\u884c\u4ee3\u7801\u5e76\u8ba1\u7b97\u65f6\u95f4\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u5b9a\u4e49\u9700\u8981\u6d4b\u8bd5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>code_to_test = &quot;&quot;&quot;<\/p>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u6d4b\u8bd5\u4ee3\u7801\u5e76\u8ba1\u7b97\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = timeit.timeit(code_to_test, number=1)<\/p>\n<p>print(f&quot;Processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528perf_counter\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/h3>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u8be6\u89e3<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165time\u6a21\u5757\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u524d\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4\u3002<\/li>\n<li>\u5728\u4ee3\u7801\u6267\u884c\u540e\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4\u3002<\/li>\n<li>\u8ba1\u7b97\u7ed3\u675f\u65f6\u95f4\u548c\u5f00\u59cb\u65f6\u95f4\u7684\u5dee\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.perf_counter()<\/p>\n<h2><strong>\u6267\u884c\u4ee3\u7801<\/strong><\/h2>\n<p>for i in range(1000000):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.perf_counter()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b9e\u8df5\u4e2d\u7684\u5904\u7406\u65f6\u95f4\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u65f6\u95f4\u7684\u8ba1\u7b97\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u7b80\u5355\u7684\u5faa\u73af\u6216\u51fd\u6570\u6267\u884c\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u590d\u6742\u7b97\u6cd5\u7684\u4f18\u5316\u3001\u6027\u80fd\u8c03\u8bd5\u548c\u7cfb\u7edf\u76d1\u63a7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7b97\u6cd5\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u6027\u80fd\u74f6\u9888\uff0c\u4ece\u800c\u8fdb\u884c\u7b97\u6cd5\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5904\u7406\u65f6\u95f4\u8ba1\u7b97\u6765\u4f18\u5316\u4e00\u4e2a\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u9700\u8981\u6392\u5e8f\u7684\u5217\u8868<\/strong><\/h2>\n<p>data = [i for i in range(10000, 0, -1)]<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u6392\u5e8f\u7b97\u6cd5<\/strong><\/h2>\n<p>data.sort()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Sorting processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6027\u80fd\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u54ea\u4e9b\u51fd\u6570\u6216\u4ee3\u7801\u6bb5\u6267\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u4ece\u800c\u8fdb\u884c\u9488\u5bf9\u6027\u7684\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u88c5\u9970\u5668\u6765\u8ba1\u7b97\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def timeit(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>        processing_time = end_time - start_time<\/p>\n<p>        print(f&quot;{func.__name__} processing time: {processing_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@timeit<\/p>\n<p>def example_function():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7cfb\u7edf\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>\u5728\u7cfb\u7edf\u76d1\u63a7\u4e2d\uff0c\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u8fd0\u7ef4\u4eba\u5458\u4e86\u89e3\u7cfb\u7edf\u5404\u4e2a\u90e8\u5206\u7684\u6027\u80fd\u8868\u73b0\uff0c\u4ece\u800c\u8fdb\u884c\u8d44\u6e90\u5206\u914d\u548c\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5904\u7406\u65f6\u95f4\u8ba1\u7b97\u6765\u76d1\u63a7\u7cfb\u7edf\u7684\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import requests<\/p>\n<p>def monitor_system(url):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    end_time = time.time()<\/p>\n<p>    processing_time = end_time - start_time<\/p>\n<p>    print(f&quot;System response time: {processing_time} seconds&quot;)<\/p>\n<p>    return response<\/p>\n<p>response = monitor_system(&quot;https:\/\/www.example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6279\u91cf\u4efb\u52a1\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u4efb\u52a1\u5904\u7406\u4e2d\uff0c\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u6574\u4e2a\u6279\u91cf\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u8fdb\u884c\u4efb\u52a1\u8c03\u5ea6\u548c\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u6279\u91cf\u4efb\u52a1\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def batch_task():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u6279\u91cf\u4efb\u52a1<\/strong><\/h2>\n<p>for _ in range(10):<\/p>\n<p>    batch_task()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Batch task processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u5e76\u53d1\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\u548c\u6027\u80fd\u63d0\u5347\u60c5\u51b5\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u591a\u7ebf\u7a0b\u4efb\u52a1\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import threading<\/p>\n<p>def thread_task():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        pass<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for _ in range(10):<\/p>\n<p>    thread = threading.Thread(target=thread_task)<\/p>\n<p>    thread.start()<\/p>\n<p>    threads.append(thread)<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Multi-threading task processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u8bad\u7ec3\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u4e2d\uff0c\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u7814\u7a76\u4eba\u5458\u4e86\u89e3\u6a21\u578b\u8bad\u7ec3\u7684\u8017\u65f6\uff0c\u4ece\u800c\u8fdb\u884c\u6a21\u578b\u4f18\u5316\u548c\u8c03\u53c2\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X, y = iris.data, iris.target<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Model tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001API\u54cd\u5e94\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u6d4b\u8bd5API\u65f6\uff0c\u8ba1\u7b97API\u7684\u54cd\u5e94\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3API\u7684\u6027\u80fd\u8868\u73b0\uff0c\u4ece\u800c\u8fdb\u884c\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97API\u7684\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import requests<\/p>\n<p>def api_request(url):<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    end_time = time.time()<\/p>\n<p>    processing_time = end_time - start_time<\/p>\n<p>    print(f&quot;API response time: {processing_time} seconds&quot;)<\/p>\n<p>    return response<\/p>\n<p>response = api_request(&quot;https:\/\/api.example.com\/data&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6570\u636e\u5e93\u67e5\u8be2\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u67e5\u8be2\u4f18\u5316\u4e2d\uff0c\u8ba1\u7b97\u67e5\u8be2\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u6027\u80fd\u74f6\u9888\uff0c\u4ece\u800c\u8fdb\u884c\u7d22\u5f15\u4f18\u5316\u548c\u67e5\u8be2\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u6570\u636e\u5e93\u67e5\u8be2\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868\u5e76\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)&#39;&#39;&#39;)<\/p>\n<p>for i in range(10000):<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;INSERT INTO test (value) VALUES (?)&#39;&#39;&#39;, (f&#39;value_{i}&#39;,))<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;SELECT * FROM test WHERE value LIKE &#39;value_%&#39; &#39;&#39;&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;Database query processing time: {processing_time} seconds&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u6587\u4ef6\u8bfb\u5199\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u8ba1\u7b97\u6587\u4ef6\u8bfb\u5199\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3I\/O\u64cd\u4f5c\u7684\u8017\u65f6\uff0c\u4ece\u800c\u8fdb\u884c\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u6587\u4ef6\u8bfb\u5199\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u751f\u6210\u5927\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;large_file.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        f.write(f&#39;line_{i}\\n&#39;)<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as f:<\/p>\n<p>    lines = f.readlines()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>processing_time = end_time - start_time<\/p>\n<p>print(f&quot;File read processing time: {processing_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u7f51\u7edc\u4f20\u8f93\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u8ba1\u7b97\u7f51\u7edc\u4f20\u8f93\u65f6\u95f4\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u6570\u636e\u4f20\u8f93\u7684\u8017\u65f6\uff0c\u4ece\u800c\u8fdb\u884c\u7f51\u7edc\u4f18\u5316\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u8ba1\u7b97\u7f51\u7edc\u4f20\u8f93\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import socket<\/p>\n<p>def send_data():<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:<\/p>\n<p>        s.connect((&#39;localhost&#39;, 12345))<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        s.sendall(b&#39;Hello, world&#39;)<\/p>\n<p>        data = s.recv(1024)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        processing_time = end_time - start_time<\/p>\n<p>        print(f&quot;Network transmission processing time: {processing_time} seconds&quot;)<\/p>\n<h2><strong>\u5047\u8bbe\u670d\u52a1\u5668\u5df2\u7ecf\u5728localhost:12345\u76d1\u542c<\/strong><\/h2>\n<p>send_data()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\uff0c\u4ece\u800c\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u548c\u8c03\u8bd5\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u5730\u8ba1\u7b97\u548c\u5206\u6790\u5904\u7406\u65f6\u95f4\u662f\u63d0\u5347\u7cfb\u7edf\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u7684\u91cd\u8981\u624b\u6bb5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u6216<code>timeit<\/code>\u6a21\u5757\u3002<code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8bb0\u5f55\u4ee3\u7801\u6267\u884c\u524d\u540e\u7684\u65f6\u95f4\u6233\u6765\u8ba1\u7b97\u65f6\u95f4\u5dee\u3002\u800c<code>timeit<\/code>\u6a21\u5757\u5219\u4e13\u95e8\u7528\u4e8e\u6d4b\u91cf\u5c0f\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u901a\u5e38\u9002\u5408\u6027\u80fd\u6d4b\u8bd5\u548c\u57fa\u51c6\u6d4b\u8bd5\u3002\u4f7f\u7528<code>timeit<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u5728\u4ee3\u7801\u4e2d\u8c03\u7528<code>timeit.timeit()<\/code>\u65b9\u6cd5\u6765\u6267\u884c\u4ee3\u7801\u5e76\u83b7\u53d6\u5e73\u5747\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u4ee3\u7801\u6027\u80fd\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>time<\/code>\u548c<code>timeit<\/code>\u6a21\u5757\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002\u4f8b\u5982\uff0c<code>cProfile<\/code>\u53ef\u4ee5\u7528\u6765\u5206\u6790\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\uff0c\u63d0\u4f9b\u8be6\u7ec6\u7684\u6267\u884c\u65f6\u95f4\u7edf\u8ba1\u3002<code>line_profiler<\/code>\u5219\u5141\u8bb8\u4f60\u9010\u884c\u5206\u6790\u4ee3\u7801\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u8bc6\u522b\u6700\u8017\u65f6\u7684\u4ee3\u7801\u6bb5\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u88c5\u9970\u5668\u6765\u8ba1\u7b97\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u88c5\u9970\u5668\u6765\u8f7b\u677e\u8ba1\u7b97\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u88c5\u9970\u5668\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u8bb0\u5f55\u51fd\u6570\u5f00\u59cb\u548c\u7ed3\u675f\u7684\u65f6\u95f4\uff0c\u8ba1\u7b97\u5e76\u8f93\u51fa\u6267\u884c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\n\ndef time_decorator(func):\n    def wrapper(*args, **kwargs):\n        start_time = time.time()\n        result = func(*args, **kwargs)\n        end_time = time.time()\n        print(f&quot;Execution time: {end_time - start_time} seconds&quot;)\n        return result\n    return wrapper\n\n@time_decorator\ndef example_function():\n    # \u8fd9\u91cc\u662f\u9700\u8981\u6d4b\u91cf\u7684\u4ee3\u7801\n    time.sleep(2)\n\nexample_function()\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u88c5\u9970\u5668\u6765\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e76\u53ef\u4ee5\u8f7b\u677e\u5e94\u7528\u4e8e\u4efb\u4f55\u5176\u4ed6\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u5904\u7406\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528timeit\u6a21\u5757\u3001\u4f7f\u7528\u7b2c [&hellip;]","protected":false},"author":3,"featured_media":1152699,"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\/1152694"}],"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=1152694"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152694\/revisions"}],"predecessor-version":[{"id":1152701,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152694\/revisions\/1152701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152699"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}