Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aa71de

Browse files
committed
Add yield keyword
1 parent f90a89d commit 7aa71de

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

howtoyield.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import memory_profiler as mem_profile
2+
import random
3+
import time
4+
5+
mem_before = mem_profile.memory_usage()[0]
6+
print(f'Before calling the function, Python is using {mem_before} MB of memory')
7+
8+
9+
def cubed_list(n):
10+
result = []
11+
for i in range(n):
12+
result.append(i ** 3)
13+
return result
14+
15+
16+
def cubed_generator(n):
17+
for i in range(n):
18+
yield i ** 3
19+
20+
21+
time_start = time.perf_counter()
22+
cubes = cubed_generator(5000000)
23+
time_end = time.perf_counter()
24+
elapsed = time_end + time_start
25+
26+
mem_after = mem_profile.memory_usage()[0]
27+
mem_usage = mem_after - mem_before
28+
29+
print(f'After calling the function, Python is using {mem_after} MB of memory')
30+
print(f'It Took {elapsed} Seconds to cube 5,000,000 integers')

0 commit comments

Comments
 (0)