mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
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:
parent
4cddb26908
commit
5c92b7390e
|
@ -90,32 +90,20 @@ def fast_primes(max_n: int) -> Generator[int, None, None]:
|
||||||
yield i
|
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__":
|
if __name__ == "__main__":
|
||||||
number = int(input("Calculate primes up to:\n>> ").strip())
|
number = int(input("Calculate primes up to:\n>> ").strip())
|
||||||
for ret in primes(number):
|
for ret in primes(number):
|
||||||
print(ret)
|
print(ret)
|
||||||
|
benchmark()
|
||||||
# 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,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user