-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgenerators.py
More file actions
38 lines (34 loc) · 1016 Bytes
/
generators.py
File metadata and controls
38 lines (34 loc) · 1016 Bytes
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
#Normal Approach
import memory_profiler
import time
def check_even(numbers):
even = []
for num in numbers:
if num % 2 == 0:
even.append(num*num)
return even
if __name__ == '__main__':
m1 = memory_profiler.memory_usage()
t1 = time.clock()
cubes = check_even(range(100000000))
t2 = time.clock()
m2 = memory_profiler.memory_usage()
time_diff = t2 - t1
mem_diff = m2[0] - m1[0]
print(f"It took {time_diff} Secs and {mem_diff} Mb to execute this method")
#Using Generators
import memory_profiler
import time
def check_even(numbers):
for num in numbers:
if num % 2 == 0:
yield num * num
if __name__ == '__main__':
m1 = memory_profiler.memory_usage()
t1 = time.clock()
cubes = check_even(range(100000000))
t2 = time.clock()
m2 = memory_profiler.memory_usage()
time_diff = t2 - t1
mem_diff = m2[0] - m1[0]
print(f"It took {time_diff} Secs and {mem_diff} Mb to execute this method")