Skip to content

Commit 20b33d8

Browse files
committed
Refactor benchmarks, add sum and harmonic
1 parent 78db2e5 commit 20b33d8

6 files changed

Lines changed: 44 additions & 21 deletions

File tree

bench/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ It's possible to run them also with gmpy2's and flint's integer types:
55
.. code:: sh
66
77
( export T="gmpy2.mpz"; \
8+
export PYTHONPATH=. ; \
89
python bench/mul.py -q --copy-env --rigorous -o $T.json )
910
1011
Beware, that the gmp prefers clang over gcc and extensions might

bench/collatz.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
# collatz.py
2-
3-
import os
1+
# bench/collatz.py
42

53
import pyperf
64

7-
if os.getenv("T") == "gmpy2.mpz":
8-
from gmpy2 import mpz
9-
elif os.getenv("T") == "flint.fmpz":
10-
from flint import fmpz as mpz
11-
elif os.getenv("T") == "int":
12-
mpz = int
13-
else:
14-
from gmp import mpz
5+
from bench.utils import mpz
156

167
zero = mpz(0)
178
one = mpz(1)

bench/harmonic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# bench/harmonic.py
2+
3+
from fractions import Fraction
4+
5+
import pyperf
6+
7+
from bench.utils import mpz, mysum
8+
9+
runner = pyperf.Runner()
10+
for n in [100, 1000]:
11+
xs = [Fraction(mpz(1), mpz(i)) for i in range(1, n + 1)]
12+
runner.bench_func(f"H({n})", mysum, xs)

bench/mul.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
# mul.py
1+
# bench/mul.py
22

3-
import os
43
from operator import mul
54

65
import pyperf
76

8-
if os.getenv("T") == "gmpy2.mpz":
9-
from gmpy2 import mpz
10-
elif os.getenv("T") == "flint.fmpz":
11-
from flint import fmpz as mpz
12-
elif os.getenv("T") == "int":
13-
mpz = int
14-
else:
15-
from gmp import mpz
7+
from bench.utils import mpz
168

179
values = ["1<<7", "1<<38", "1<<300", "1<<3000"]
1810

bench/sum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# bench/sum.py
2+
3+
import pyperf
4+
5+
from bench.utils import mpz, mysum
6+
7+
runner = pyperf.Runner()
8+
for n in [100, 1000]:
9+
xs = [mpz(i) for i in range(1, n + 1)]
10+
runner.bench_func(f"sum({n})", mysum, xs)

bench/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
if os.getenv("T") == "gmpy2.mpz":
4+
from gmpy2 import mpz
5+
elif os.getenv("T") == "flint.fmpz":
6+
from flint import fmpz as mpz
7+
elif os.getenv("T") == "int":
8+
mpz = int
9+
else:
10+
pass
11+
12+
13+
def mysum(xs):
14+
total = xs[0]
15+
for t in xs[1:]:
16+
total += t
17+
return t

0 commit comments

Comments
 (0)