Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # global_vs_local.py
- from time import perf_counter
- global_var = 10
- ttt = range(5_000_000)
- for i in '.'*50:
- start = perf_counter()
- def local_func():
- ans = 0
- local_var = global_var
- for i in ttt:
- ans += global_var
- return ans
- local_func()
- local_time = perf_counter()-start
- start = perf_counter()
- def global_func():
- ans = 0
- for i in ttt:
- ans += global_var
- return ans
- global_func()
- global_time = perf_counter()-start
- print(f"")
- print(f"local_time = {local_time:0f}")
- print(f"global_time = {global_time:0f}")
- a, b = 'local_func', 'global_func'
- t = global_time-local_time
- if local_time > global_time:
- a, b = b, a
- t = local_time-global_time
- print(f"{a} is {t:0f} seconds faster then {b}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement