Skip to content

Commit

Permalink
Add gmpy2+numba benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tuokri committed Jul 11, 2024
1 parent fa1c8af commit 75fce37
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
*.log

**/.python-version

DevUtils/.ipynb_checkpoints/
1 change: 0 additions & 1 deletion DevUtils/gmp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def calculate(self, cmd_data: str):
x = gmpy2.mpz_urandomb(self.rng, a - 1)
x = x.bit_set(0)
x = x.bit_set(a - 1)
# if x.is_probab_prime(50):
if x.is_prime(50):
x -= 1
if x.is_divisible(65537):
Expand Down
131 changes: 131 additions & 0 deletions DevUtils/numba_gmpy2_benchmarks.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "f9658e34-04f9-439c-a54b-f9b17a4730e1",
"metadata": {},
"outputs": [],
"source": [
"import gmpy2\n",
"import numba\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5fec4995-b398-41e2-a267-3ce325c71ada",
"metadata": {},
"outputs": [],
"source": [
"rng = gmpy2.random_state(int(time.time()))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "bc392285-2d91-4553-b30c-8bb8f784330c",
"metadata": {},
"outputs": [],
"source": [
"def rand_prime(rng: gmpy2.mpz, a: int) -> gmpy2.mpz:\n",
" while True:\n",
" x = gmpy2.mpz_urandomb(rng, a - 1)\n",
" x = x.bit_set(0)\n",
" x = x.bit_set(a - 1)\n",
" if x.is_prime(50):\n",
" x -= 1\n",
" if x.is_divisible(65537):\n",
" continue\n",
" x += 1\n",
" return x\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a513076a-e856-4fc3-9469-52644ed90e94",
"metadata": {},
"outputs": [],
"source": [
"@numba.jit(forceobj=True)\n",
"def rand_prime_numba(x: gmpy2.mpz, a: int) -> gmpy2.mpz:\n",
" while True:\n",
" # x = gmpy2.mpz_urandomb(self.rng, a - 1)\n",
" x = x.bit_set(0)\n",
" x = x.bit_set(a - 1)\n",
" if x.is_prime(50):\n",
" x -= 1\n",
" if x.is_divisible(65537):\n",
" continue\n",
" x += 1\n",
" return x\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "7e8aab78-7c4e-4ef7-a7b8-0be2869acb33",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.12 ms ± 106 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(2, 128 + 1):\n",
" rand_prime(rng, i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51e89e7d-c3e8-4ae4-b8c6-73d79c40e9e4",
"metadata": {},
"outputs": [],
"source": [
"%%timeit\n",
"for i in range(2, 128 + 1):\n",
" x = gmpy2.mpz_urandomb(rng, i - 1)\n",
" rand_prime_numba(x, i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e92ef786-8594-4f07-a13c-adc573dc2fb0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions DevUtils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
gmpy2==2.2.0
loguru==0.7.2
numba==0.60.0
numpy==2.0.0
pytest==8.2.2

0 comments on commit 75fce37

Please sign in to comment.