-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeephole.py
executable file
·74 lines (56 loc) · 1.54 KB
/
Peephole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 24 22:03:41 2021
@author: maherme
"""
#%%
def my_func():
a = 24 * 60
b = (1, 2) * 5
c = 'abc' * 3
d = 'ab' * 11
e = 'the quick brown fox' * 5
f = ['a', 'b'] * 3
print(my_func.__code__.co_consts) # Notice some items are precalculated
#%%
# Notice the difference in the way to store items for a list and a set
def my_func(e):
if e in [1, 2, 3]:
pass
print(my_func.__code__.co_consts) # Notice the list becomes a tuple
#%%
def my_func(e):
if e in {1, 2, 3}:
pass
print(my_func.__code__.co_consts) # Notice the set becomes a frozenset
#%%
# Let's see a performance in time about a searching in a list, a tuple and a
# set.
import string
import time
print(string.ascii_letters)
char_list = list(string.ascii_letters)
print(char_list)
char_tuple = tuple(string.ascii_letters)
print(char_tuple)
# Notice that set does not contain repeated items and does not keep the order
char_set = set(string.ascii_letters)
print(char_set)
def membership_test(n, container):
for i in range(n):
if 'z' in container:
pass
start = time.perf_counter()
membership_test(1_000_000, char_list)
end = time.perf_counter()
print('list', end-start)
start = time.perf_counter()
membership_test(1_000_000, char_tuple)
end = time.perf_counter()
print('tuple', end-start)
start = time.perf_counter()
membership_test(1_000_000, char_set)
end = time.perf_counter()
print('set', end-start) # Notice the set is much faster than the others
#%%