{"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
Python\u57fa\u51c6\u5206\u6790\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n ncalls <\/b>-\u8fd9\u662f\u62e8\u6253\u7684\u7535\u8bdd\u6570\u3002<\/span>\u4ec0\u4e48\u662f\u57fa\u51c6\u6d4b\u8bd5\uff1f<\/h2>\n
\u57fa\u51c6\u6d4b\u8bd5\u5982\u4f55\u5de5\u4f5c\uff1f<\/h3>\n
\u7528\u4e8e\u57fa\u51c6\u6d4b\u8bd5\u7684Python\u6a21\u5757<\/h3>\n
\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 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 # 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\u8f93\u51fa<\/h3>\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 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 # 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\u8f93\u51fa<\/h3>\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
cProfile \u2013\u5185\u7f6e\u6a21\u5757<\/h2>\n
\u793a\u4f8b<\/h3>\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 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 # 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 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}]}}