-
Notifications
You must be signed in to change notification settings - Fork 0
/
faster_python.py
169 lines (131 loc) · 2.96 KB
/
faster_python.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import time
import random
n = 1000000
rng = range(1, n)
nr = [random.randint(0, 9) for i in rng]
# faster algorithms
start = time.time()
sum = 0
for x in range(1, n+1):
sum += x
print(sum)
print(time.time() - start)
start = time.time()
print(n*(1+n)/2)
print(time.time() - start)
# faster python (code structures)
### 0 - create list/dict
start = time.time()
l1 = list() # dict()
print(time.time() - start)
start = time.time()
l2 = [] # {}
print(time.time() - start)
### 1 - count elements in list
start = time.time()
how_many = 0
for element in rng:
how_many += 1
print(how_many)
print(time.time() - start)
start = time.time()
print(len(rng))
print(time.time() - start)
### 2 - filter a list
start = time.time()
output = []
for element in rng:
if element % 2:
output.append(element)
print(time.time() - start)
start = time.time()
list(filter(lambda x: x % 2, rng))
print(time.time() - start)
start = time.time()
[item for item in rng if item % 2]
print(time.time() - start)
### 3 - item in list
def check_number(number):
for item in rng:
if item == number:
return True
return False
start = time.time()
check_number(125)
print(time.time() - start)
start = time.time()
check_number(99999) # speed depends on item position => no constant lookup time
print(time.time() - start)
# use set for constant lookup time
MILLION_SET = set(rng)
start = time.time()
125 in MILLION_SET
print(time.time() - start)
start = time.time()
99999 in MILLION_SET
print(time.time() - start)
### 4 - remove duplicates
start = time.time()
unique = []
for item in nr:
if item not in unique:
unique.append(item)
print(time.time() - start)
start = time.time()
set(nr) # set conversion removes duplicates
print(time.time() - start)
### 5 - list sorting
start = time.time()
sorted(nr)
print(time.time() - start)
start = time.time()
nr.sort()
print(time.time() - start)
### 6 - apply function
def square(number):
return number**2
start = time.time()
squares = [square(i) for i in rng] # call function i times
print(time.time() - start)
def squares():
return [i**2 for i in rng] # call once
start = time.time()
squares()
print(time.time() - start)
### 7 - function vs lambda
def greet(name):
return f'Hello {name}'
start = time.time()
greet('John')
print(time.time() - start)
lgreet = lambda name: f'Hello {name}'
start = time.time()
lgreet('John')
print(time.time() - start)
### 8 - check truthy
variable = True
start = time.time()
if variable == True:
print('hello')
print(time.time() - start)
start = time.time()
if variable is True:
print('hello')
print(time.time() - start)
start = time.time()
if variable:
print('hello')
print(time.time() - start)
# falsy (False, None, zero, empty string, empty list)
start = time.time()
if len(nr) == 0:
print('hello')
print(time.time() - start)
start = time.time()
if nr == []:
print('hello')
print(time.time() - start)
start = time.time()
if not nr:
print('hello')
print(time.time() - start)