-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathprofile_demo.py
More file actions
37 lines (28 loc) · 765 Bytes
/
profile_demo.py
File metadata and controls
37 lines (28 loc) · 765 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
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description: 性能工具
"""
# python -m cProfile profile_demo.py
def is_prime(num):
for factor in range(2, int(num ** 0.5) + 1):
if num % factor == 0:
return False
return True
class PrimeIter:
def __init__(self, total):
self.counter = 0
self.current = 1
self.total = total
def __iter__(self):
return self
def __next__(self):
if self.counter < self.total:
self.current += 1
while not is_prime(self.current):
self.current += 1
self.counter += 1
return self.current
raise StopIteration()
if __name__ == '__main__':
list(PrimeIter(10000))