prime_numbers.py: Tighten up the benchmarks (#7976)

* prime_numbers.py: Tighten up the benchmarks

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Christian Clauss 2022-11-10 03:42:14 +01:00 committed by GitHub
parent 4cddb26908
commit 5c92b7390e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -90,32 +90,20 @@ def fast_primes(max_n: int) -> Generator[int, None, None]:
yield i
def benchmark():
"""
Let's benchmark our functions side-by-side...
"""
from timeit import timeit
setup = "from __main__ import slow_primes, primes, fast_primes"
print(timeit("slow_primes(1_000_000_000_000)", setup=setup, number=1_000_000))
print(timeit("primes(1_000_000_000_000)", setup=setup, number=1_000_000))
print(timeit("fast_primes(1_000_000_000_000)", setup=setup, number=1_000_000))
if __name__ == "__main__":
number = int(input("Calculate primes up to:\n>> ").strip())
for ret in primes(number):
print(ret)
# Let's benchmark them side-by-side...
from timeit import timeit
print(
timeit(
"slow_primes(1_000_000_000_000)",
setup="from __main__ import slow_primes",
number=1_000_000,
)
)
print(
timeit(
"primes(1_000_000_000_000)",
setup="from __main__ import primes",
number=1_000_000,
)
)
print(
timeit(
"fast_primes(1_000_000_000_000)",
setup="from __main__ import fast_primes",
number=1_000_000,
)
)
benchmark()