{"id":1582,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u57fa\u51c6\u5206\u6790","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1582.html","title":{"rendered":"Python\u57fa\u51c6\u5206\u6790"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u57fa\u51c6\u5206\u6790<\/h1>\n

Python\u57fa\u51c6\u5206\u6790\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u57fa\u51c6\u6d4b\u8bd5\u548c\u6027\u80fd\u5206\u6790\u5982\u4f55\u5e2e\u52a9\u89e3\u51b3\u6027\u80fd\u95ee\u9898\u3002\n <\/div>\n
\n \u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u7f16\u5199\u4e86\u4e00\u4e2a\u4ee3\u7801\uff0c\u5e76\u4e14\u5b83\u4e5f\u7ed9\u51fa\u4e86\u9884\u671f\u7684\u7ed3\u679c\uff0c\u4f46\u662f\u5982\u679c\u7531\u4e8e\u9700\u6c42\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u6211\u4eec\u5e0c\u671b\u66f4\u5feb\u5730\u8fd0\u884c\u6b64\u4ee3\u7801\uff0c\u8be5\u600e\u4e48\u529e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u627e\u51fa\u4ee3\u7801\u7684\u54ea\u4e9b\u90e8\u5206\u4f7f\u6574\u4e2a\u7a0b\u5e8f\u53d8\u6162\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u57fa\u51c6\u6d4b\u8bd5\u548c\u6027\u80fd\u5206\u6790\u4f1a\u5f88\u6709\u7528\u3002\n <\/div>\n

\u4ec0\u4e48\u662f\u57fa\u51c6\u6d4b\u8bd5\uff1f<\/h2>\n
\n \u57fa\u51c6\u6d4b\u8bd5\u65e8\u5728\u901a\u8fc7\u200b\u200b\u4e0e\u6807\u51c6\u6bd4\u8f83\u6765\u8bc4\u4f30\u67d0\u4e9b\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u8fd9\u91cc\u51fa\u73b0\u7684\u95ee\u9898\u662f\uff0c\u57fa\u51c6\u6d4b\u8bd5\u662f\u4ec0\u4e48\u4ee5\u53ca\u5728\u8f6f\u4ef6\u7f16\u7a0b\u7684\u60c5\u51b5\u4e0b\u4e3a\u4ec0\u4e48\u9700\u8981\u57fa\u51c6\u6d4b\u8bd5\u3002\u5bf9\u4ee3\u7801\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\u610f\u5473\u7740\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u6709\u591a\u5feb\uff0c\u74f6\u9888\u5728\u54ea\u91cc\u3002\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\u7684\u4e3b\u8981\u539f\u56e0\u4e4b\u4e00\u662f\u5b83\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u3002\n <\/div>\n

\u57fa\u51c6\u6d4b\u8bd5\u5982\u4f55\u5de5\u4f5c\uff1f<\/h3>\n
\n \u5982\u679c\u6211\u4eec\u8c08\u8bba\u57fa\u51c6\u6d4b\u8bd5\u7684\u5de5\u4f5c\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u6574\u4e2a\u7a0b\u5e8f\u4f5c\u4e3a\u4e00\u4e2a\u5f53\u524d\u72b6\u6001\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u7ec4\u5408\u5fae\u57fa\u51c6\u6d4b\u8bd5\uff0c\u7136\u540e\u5c06\u7a0b\u5e8f\u5206\u89e3\u4e3a\u8f83\u5c0f\u7684\u7a0b\u5e8f\u3002\u4e3a\u4e86\u627e\u5230\u6211\u4eec\u7a0b\u5e8f\u4e2d\u7684\u74f6\u9888\u5e76\u5bf9\u5176\u8fdb\u884c\u4f18\u5316\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u7406\u89e3\u4e3a\u5c06\u5927\u800c\u68d8\u624b\u7684\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u8f83\u5c0f\u548c\u8f83\u5bb9\u6613\u89e3\u51b3\u7684\u95ee\u9898\uff0c\u4ee5\u5bf9\u5176\u8fdb\u884c\u4f18\u5316\u3002\n <\/div>\n

\u7528\u4e8e\u57fa\u51c6\u6d4b\u8bd5\u7684Python\u6a21\u5757<\/h3>\n
\n \u5728Python\u4e2d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u6a21\u5757\uff0c\u79f0\u4e3a
\n timeit <\/b>\u3002\u501f\u52a9
\n timeit <\/b>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u6d4b\u91cf\u5c11\u91cfPython\u4ee3\u7801\u7684\u6027\u80fd\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u5728\u4e0b\u9762\u7684Python\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u5c06\u5bfc\u5165
\n timeit <\/b>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u5c06\u8fdb\u4e00\u6b65\u6d4b\u91cf\u6267\u884c\u4e24\u4e2a\u529f\u80fd-
\n functionA <\/b>\u548c
\n functionB <\/b>-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>timeit
import <\/span>time
def <\/span>functionA():
   print(\"Function A starts the execution:\"<\/span>)
   print(\"Function A completes the execution:\"<\/span>)
def <\/span>functionB():
   print(\"Function B starts the execution\"<\/span>)
   print(\"Function B completes the execution\"<\/span>)
start_time = timeit.default_timer<\/span>()
functionA()
print(timeit.default_timer<\/span>() - start_time)
start_time = timeit.default_timer<\/span>()
functionB()
print(timeit.default_timer<\/span>() - start_time)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u4e0a\u8ff0\u811a\u672c\u540e\uff0c\u6211\u4eec\u5c06\u83b7\u5f97\u4e24\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5982\u4e0b\u6240\u793a\u3002\n <\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Function A starts the execution:
Function A completes the execution:
0.0014599495514175942
Function B starts the execution
Function B completes the execution
0.0017024724827479076
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4f7f\u7528\u88c5\u9970\u5668\u529f\u80fd\u7f16\u5199\u81ea\u5df1\u7684\u8ba1\u65f6\u5668<\/h2>\n
\n \u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u8ba1\u65f6\u5668\uff0c\u5176\u4f5c\u7528\u7c7b\u4f3c\u4e8e
\n timeit <\/b>\u6a21\u5757\u3002\u53ef\u4ee5\u5728
\n decorator <\/b>\u51fd\u6570\u7684\u5e2e\u52a9\u4e0b\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u81ea\u5b9a\u4e49\u8ba1\u65f6\u5668\u7684\u793a\u4f8b-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>random
import <\/span>time
def <\/span>timer_func(func):
   def <\/span>function_timer(*args, **kwargs):
   start = time.time<\/span>()
   value = func(*args, **kwargs)
   end = time.time<\/span>()
   runtime = end - start
   msg = \"{func} took {time} seconds to complete its execution.\"<\/span>
      print(msg.format<\/span>(func = func.__name__,time = runtime))
   return <\/span>value
   return <\/span>function_timer
@timer_func
def <\/span>Myfunction():
   for <\/span><\/span>x in <\/span>range(5):
   sleep_time = random.choice<\/span>(range(1,3))
   time.sleep<\/span>(sleep_time)
if <\/span>__name__ == '__main__'<\/span>:
   Myfunction()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0apython\u811a\u672c\u6709\u52a9\u4e8e\u5bfc\u5165\u968f\u673a\u65f6\u95f4\u6a21\u5757\u3002\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86timer_func()\u88c5\u9970\u5668\u51fd\u6570\u3002\u5b83\u5185\u90e8\u5177\u6709function_timer()\u51fd\u6570\u3002\u73b0\u5728\uff0c\u5d4c\u5957\u51fd\u6570\u5c06\u5728\u8c03\u7528\u4f20\u5165\u7684\u51fd\u6570\u4e4b\u524d\u62a2\u5360\u65f6\u95f4\u3002\u7136\u540e\uff0c\u5b83\u7b49\u5f85\u51fd\u6570\u8fd4\u56de\u5e76\u83b7\u53d6\u7ed3\u675f\u65f6\u95f4\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u6700\u7ec8\u53ef\u4ee5\u4f7fpython\u811a\u672c\u6253\u5370\u6267\u884c\u65f6\u95f4\u3002\u8be5\u811a\u672c\u5c06\u751f\u6210\u5982\u4e0b\u6240\u793a\u7684\u8f93\u51fa\u3002\n <\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Myfunction took 8.000457763671875 seconds to complete its execution.
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4ec0\u4e48\u662f\u914d\u7f6e\u6587\u4ef6\uff1f<\/h2>\n
\n \u6709\u65f6\uff0c\u7a0b\u5e8f\u5458\u60f3\u8861\u91cf\u4e00\u4e9b\u5c5e\u6027\uff0c\u4f8b\u5982\u5185\u5b58\u4f7f\u7528\uff0c\u65f6\u95f4\u590d\u6742\u6027\u6216\u6709\u5173\u7a0b\u5e8f\u7684\u7279\u5b9a\u6307\u4ee4\u7684\u4f7f\u7528\uff0c\u4ee5\u8861\u91cf\u8be5\u7a0b\u5e8f\u7684\u5b9e\u9645\u529f\u80fd\u3002\u8fd9\u79cd\u5173\u4e8e\u7a0b\u5e8f\u7684\u5ea6\u91cf\u79f0\u4e3a\u6982\u8981\u5206\u6790\u3002\u6982\u8981\u5206\u6790\u4f7f\u7528\u52a8\u6001\u7a0b\u5e8f\u5206\u6790\u6765\u8fdb\u884c\u6b64\u7c7b\u6d4b\u91cf\u3002\n <\/div>\n
\n \u5728\u968f\u540e\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u7528\u4e8e\u5206\u6790\u7684\u4e0d\u540cPython\u6a21\u5757\u3002\n <\/div>\n

cProfile \u2013\u5185\u7f6e\u6a21\u5757<\/h2>\n
\n cProfile <\/b>\u662f\u7528\u4e8e\u5206\u6790\u7684Python\u5185\u7f6e\u6a21\u5757\u3002\u8be5\u6a21\u5757\u662fC\u6269\u5c55\uff0c\u5177\u6709\u5408\u7406\u7684\u5f00\u9500\uff0c\u4f7f\u5176\u9002\u5408\u4e8e\u5206\u6790\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u8fd0\u884c\u540e\uff0c\u5b83\u5c06\u8bb0\u5f55\u6240\u6709\u529f\u80fd\u548c\u6267\u884c\u65f6\u95f4\u3002\u5b83\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u6709\u65f6\u96be\u4ee5\u89e3\u91ca\u548c\u91c7\u53d6\u884c\u52a8\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e0a\u4f7f\u7528cProfile-\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
def <\/span>increment_global<\/span>():
   global<\/span> x
   x += 1
def <\/span>taskofThread(lock):
   for <\/span><\/span>_ in <\/span>range(50000):
   lock.acquire<\/span>()
   increment_global<\/span>()
   lock.release<\/span>()
def <\/span>main():
   global<\/span> x
   x = 0
   lock = threading.Lock<\/span>()
   t1 = threading.Thread<\/span>(target=taskofThread, args=(lock,))
   t2 = threading.Thread<\/span>(target= taskofThread, args=(lock,))
   t1.start<\/span>()
   t2.start<\/span>()
   t1.join<\/span>()
   t2.join<\/span>()
if <\/span>__name__ == \"__main__\"<\/span>:
   for <\/span><\/span>i in <\/span>range(5):
      main()
   print(\"x = {1} after Iteration {0}\"<\/span>.format<\/span>(i,x))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728
\n thread_increment.py <\/b>\u6587\u4ef6\u4e2d\u3002\u73b0\u5728\uff0c\u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528cProfile\u6267\u884c\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
(base) D:\\ProgramData>python -m cProfile thread_increment.py
x = 100000 after Iteration 0
x = 100000 after Iteration 1
x = 100000 after Iteration 2
x = 100000 after Iteration 3
x = 100000 after Iteration 4
      3577 function calls (3522 primitive calls) in <\/span>1.688 seconds
   Ordered by: standard name
   ncalls tottime percall cumtime percall filename:lineno(function)
   5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:103<\/span>(release)
   5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:143<\/span>(__init__)
   5 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:147<\/span>(__enter__)
   \u2026 \u2026 \u2026 \u2026
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ece\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230cProfile\u6253\u5370\u51fa\u6240\u67093577\u4e2a\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u51fd\u6570\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u548c\u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002\u4ee5\u4e0b\u662f\u6211\u4eec\u5728\u8f93\u51fa\u4e2d\u83b7\u5f97\u7684\u5217-\n <\/div>\n

ncalls <\/b>-\u8fd9\u662f\u62e8\u6253\u7684\u7535\u8bdd\u6570\u3002<\/span>
\n tottime <\/b>-\u8fd9\u662f\u5728\u7ed9\u5b9a\u529f\u80fd\u4e2d\u82b1\u8d39\u7684\u603b\u65f6\u95f4\u3002<\/span>
\n percall <\/b>-\u6307tottime\u9664\u4ee5ncall\u7684\u5546\u3002<\/span>
\n cumtime <\/b>-\u5b83\u662f\u5728\u6b64\u5b50\u529f\u80fd\u548c\u6240\u6709\u5b50\u529f\u80fd\u4e2d\u82b1\u8d39\u7684\u7d2f\u79ef\u65f6\u95f4\u3002\u9012\u5f52\u51fd\u6570\u751a\u81f3\u66f4\u51c6\u786e\u3002<\/span>
\n percall <\/b>-\u5b83\u662fcumtime\u9664\u4ee5\u539f\u59cb\u8c03\u7528\u7684\u5546\u3002<\/span>
\n filename\uff1alineno\uff08function\uff09<\/b>-\u57fa\u672c\u4e0a\u63d0\u4f9b\u6bcf\u4e2a\u51fd\u6570\u7684\u76f8\u5e94\u6570\u636e\u3002<\/span>
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u57fa\u51c6\u5206\u6790zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-1582","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1582"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1582"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1582\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}