Skip to content

Commit 5c92b73

Browse files
cclaussgithub-actions
and
github-actions
authored
prime_numbers.py: Tighten up the benchmarks (TheAlgorithms#7976)
* prime_numbers.py: Tighten up the benchmarks * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4cddb26 commit 5c92b73

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

maths/prime_numbers.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,20 @@ def fast_primes(max_n: int) -> Generator[int, None, None]:
9090
yield i
9191

9292

93+
def benchmark():
94+
"""
95+
Let's benchmark our functions side-by-side...
96+
"""
97+
from timeit import timeit
98+
99+
setup = "from __main__ import slow_primes, primes, fast_primes"
100+
print(timeit("slow_primes(1_000_000_000_000)", setup=setup, number=1_000_000))
101+
print(timeit("primes(1_000_000_000_000)", setup=setup, number=1_000_000))
102+
print(timeit("fast_primes(1_000_000_000_000)", setup=setup, number=1_000_000))
103+
104+
93105
if __name__ == "__main__":
94106
number = int(input("Calculate primes up to:\n>> ").strip())
95107
for ret in primes(number):
96108
print(ret)
97-
98-
# Let's benchmark them side-by-side...
99-
from timeit import timeit
100-
101-
print(
102-
timeit(
103-
"slow_primes(1_000_000_000_000)",
104-
setup="from __main__ import slow_primes",
105-
number=1_000_000,
106-
)
107-
)
108-
print(
109-
timeit(
110-
"primes(1_000_000_000_000)",
111-
setup="from __main__ import primes",
112-
number=1_000_000,
113-
)
114-
)
115-
print(
116-
timeit(
117-
"fast_primes(1_000_000_000_000)",
118-
setup="from __main__ import fast_primes",
119-
number=1_000_000,
120-
)
121-
)
109+
benchmark()

0 commit comments

Comments
 (0)