-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path17_generators.py
49 lines (40 loc) · 958 Bytes
/
17_generators.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
# Normal method
import memory_profiler
def generate_even_squares(numbers):
even_list = []
for num in numbers:
if num % 2 == 0:
even_list.append(num*num)
return even_list
m1 = memory_profiler.memory_usage()
es = generate_even_squares(range(30000000))
counter=0
for x in es:
if counter > 10:
break
print(x)
counter = counter + 1
m2 = memory_profiler.memory_usage()
print("Memory used (mb): ", round(m2[0]-m1[0]))
# Generator method
import memory_profiler
def generate_even_squares(numbers):
even_list = []
for num in numbers:
if num % 2 == 0:
yield (num*num)
m1 = memory_profiler.memory_usage()
es = generate_even_squares(range(10000))
'''
print(next(es))
print(next(es))
print(next(es))
'''
counter=0
for x in es:
if counter > 10:
break
print(x)
counter = counter + 1
m2 = memory_profiler.memory_usage()
print("Memory used (mb): ", round(m2[0]-m1[0]))