Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f90a89d commit 7aa71deCopy full SHA for 7aa71de
howtoyield.py
@@ -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
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