CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/公开课/文档/年薪50W+的Python程序员如何写代码/code/Python/opencourse/part04/example.py
Views: 729
1
import cProfile
2
3
4
# @profile
5
def is_prime(num):
6
for factor in range(2, int(num ** 0.5) + 1):
7
if num % factor == 0:
8
return False
9
return True
10
11
12
class PrimeIter:
13
14
def __init__(self, total):
15
self.counter = 0
16
self.current = 1
17
self.total = total
18
19
def __iter__(self):
20
return self
21
22
def __next__(self):
23
if self.counter < self.total:
24
self.current += 1
25
while not is_prime(self.current):
26
self.current += 1
27
self.counter += 1
28
return self.current
29
raise StopIteration()
30
31
32
@profile
33
def eat_memory():
34
items = []
35
for _ in range(1000000):
36
items.append(object())
37
return items
38
39
40
def main():
41
eat_memory()
42
# list(PrimeIter(1000))
43
# cProfile.run('list(PrimeIter(10000))')
44
45
46
if __name__ == '__main__':
47
main()
48
49