Skip to content

Commit 75fce37

Browse files
committed
Add gmpy2+numba benchmarks
1 parent fa1c8af commit 75fce37

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
*.log
1111

1212
**/.python-version
13+
14+
DevUtils/.ipynb_checkpoints/

DevUtils/gmp_server.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def calculate(self, cmd_data: str):
148148
x = gmpy2.mpz_urandomb(self.rng, a - 1)
149149
x = x.bit_set(0)
150150
x = x.bit_set(a - 1)
151-
# if x.is_probab_prime(50):
152151
if x.is_prime(50):
153152
x -= 1
154153
if x.is_divisible(65537):

DevUtils/numba_gmpy2_benchmarks.ipynb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"id": "f9658e34-04f9-439c-a54b-f9b17a4730e1",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"import gmpy2\n",
11+
"import numba\n",
12+
"import time"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 6,
18+
"id": "5fec4995-b398-41e2-a267-3ce325c71ada",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"rng = gmpy2.random_state(int(time.time()))"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 25,
28+
"id": "bc392285-2d91-4553-b30c-8bb8f784330c",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"def rand_prime(rng: gmpy2.mpz, a: int) -> gmpy2.mpz:\n",
33+
" while True:\n",
34+
" x = gmpy2.mpz_urandomb(rng, a - 1)\n",
35+
" x = x.bit_set(0)\n",
36+
" x = x.bit_set(a - 1)\n",
37+
" if x.is_prime(50):\n",
38+
" x -= 1\n",
39+
" if x.is_divisible(65537):\n",
40+
" continue\n",
41+
" x += 1\n",
42+
" return x\n",
43+
" break"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 29,
49+
"id": "a513076a-e856-4fc3-9469-52644ed90e94",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"@numba.jit(forceobj=True)\n",
54+
"def rand_prime_numba(x: gmpy2.mpz, a: int) -> gmpy2.mpz:\n",
55+
" while True:\n",
56+
" # x = gmpy2.mpz_urandomb(self.rng, a - 1)\n",
57+
" x = x.bit_set(0)\n",
58+
" x = x.bit_set(a - 1)\n",
59+
" if x.is_prime(50):\n",
60+
" x -= 1\n",
61+
" if x.is_divisible(65537):\n",
62+
" continue\n",
63+
" x += 1\n",
64+
" return x\n",
65+
" break"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 27,
71+
"id": "7e8aab78-7c4e-4ef7-a7b8-0be2869acb33",
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"name": "stdout",
76+
"output_type": "stream",
77+
"text": [
78+
"5.12 ms ± 106 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"%%timeit\n",
84+
"for i in range(2, 128 + 1):\n",
85+
" rand_prime(rng, i)"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "51e89e7d-c3e8-4ae4-b8c6-73d79c40e9e4",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"%%timeit\n",
96+
"for i in range(2, 128 + 1):\n",
97+
" x = gmpy2.mpz_urandomb(rng, i - 1)\n",
98+
" rand_prime_numba(x, i)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"id": "e92ef786-8594-4f07-a13c-adc573dc2fb0",
105+
"metadata": {},
106+
"outputs": [],
107+
"source": []
108+
}
109+
],
110+
"metadata": {
111+
"kernelspec": {
112+
"display_name": "Python 3 (ipykernel)",
113+
"language": "python",
114+
"name": "python3"
115+
},
116+
"language_info": {
117+
"codemirror_mode": {
118+
"name": "ipython",
119+
"version": 3
120+
},
121+
"file_extension": ".py",
122+
"mimetype": "text/x-python",
123+
"name": "python",
124+
"nbconvert_exporter": "python",
125+
"pygments_lexer": "ipython3",
126+
"version": "3.12.3"
127+
}
128+
},
129+
"nbformat": 4,
130+
"nbformat_minor": 5
131+
}

DevUtils/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
gmpy2==2.2.0
22
loguru==0.7.2
3+
numba==0.60.0
34
numpy==2.0.0
45
pytest==8.2.2

0 commit comments

Comments
 (0)