renumbering chapters >= 19
This commit is contained in:
34
19-concurrency/primes/sequential.py
Normal file
34
19-concurrency/primes/sequential.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
sequential.py: baseline for comparing sequential, multiprocessing,
|
||||
and threading code for CPU-intensive work.
|
||||
"""
|
||||
|
||||
from time import perf_counter
|
||||
from typing import NamedTuple
|
||||
|
||||
from primes import is_prime, NUMBERS
|
||||
|
||||
class Result(NamedTuple): # <1>
|
||||
prime: bool
|
||||
elapsed: float
|
||||
|
||||
def check(n: int) -> Result: # <2>
|
||||
t0 = perf_counter()
|
||||
prime = is_prime(n)
|
||||
return Result(prime, perf_counter() - t0)
|
||||
|
||||
def main() -> None:
|
||||
print(f'Checking {len(NUMBERS)} numbers sequentially:')
|
||||
t0 = perf_counter()
|
||||
for n in NUMBERS: # <3>
|
||||
prime, elapsed = check(n)
|
||||
label = 'P' if prime else ' '
|
||||
print(f'{n:16} {label} {elapsed:9.6f}s')
|
||||
|
||||
elapsed = perf_counter() - t0 # <4>
|
||||
print(f'Total time: {elapsed:.2f}s')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user