-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmembership2.py
More file actions
51 lines (42 loc) · 1.77 KB
/
Copy pathmembership2.py
File metadata and controls
51 lines (42 loc) · 1.77 KB
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
"""
Check if a specific number exists in a list vs. in a set
Element at the beginning of the list
python -m timeit -s "from membership2 import test_in" "test_in(42)"
301 nsec
python -m timeit -s "from membership2 import test_in_set" "test_in_set(42)"
45.9 nsec (301/45.9 = 6.56)
python -m timeit -s "from membership2 import test_in_set_proper" "test_in_set_proper(42)"
11.8 msec (11800000/301 = 39,203)
Element at the end of the list
python -m timeit -s "from membership2 import test_in" "test_in(999_958)"
6.04 msec
python -m timeit -s "from membership2 import test_in_set" "test_in_set(999_958)"
51.5 nsec (6040000/51.5 = 117,282)
python -m timeit -s "from membership2 import test_in_set_proper" "test_in_set_proper(999_958)"
11.9 msec (11.9/6.04 = 1.97)
Element does not exist:
python -m timeit -s "from membership2 import test_in" "test_in(-5)"
5.87 msec
python -m timeit -s "from membership2 import test_in_set" "test_in_set(-5)"
46.1 nsec (5870000/46.1 = 127,332)
python -m timeit -s "from membership2 import test_in_set_proper" "test_in_set_proper(-5)"
11.8 msec (11.8/5.87 = 2.01)
How long does it take convert a list to a set?
python -m timeit -s "MILLION_NUMBERS = list(range(1_000_000))" "set(MILLION_NUMBERS)"
11.8 msec
Converting list to a set takes most of the time in the previous benchmarks.
How long does it take to create a set vs. create a list?
python -m timeit "list(range(1_000_000))"
13 msec
python -m timeit "set(range(1_000_000))"
21.7 msec
Creating set is much slower than creating a list
"""
MILLION_NUMBERS = list(range(1_000_000))
MILLION_NUMBERS_SET = set(MILLION_NUMBERS)
def test_in(number):
return number in MILLION_NUMBERS
def test_in_set(number):
return number in MILLION_NUMBERS_SET
def test_in_set_proper(number):
return number in set(MILLION_NUMBERS)