From 75fce37887ba099d936062df98ae412cf07f9687 Mon Sep 17 00:00:00 2001 From: Tuomo Kriikkula Date: Thu, 11 Jul 2024 16:56:50 +0300 Subject: [PATCH] Add gmpy2+numba benchmarks --- .gitignore | 2 + DevUtils/gmp_server.py | 1 - DevUtils/numba_gmpy2_benchmarks.ipynb | 131 ++++++++++++++++++++++++++ DevUtils/requirements.txt | 1 + 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 DevUtils/numba_gmpy2_benchmarks.ipynb diff --git a/.gitignore b/.gitignore index 22f89b9..5d47b49 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ *.log **/.python-version + +DevUtils/.ipynb_checkpoints/ diff --git a/DevUtils/gmp_server.py b/DevUtils/gmp_server.py index 95313b1..350cd4e 100644 --- a/DevUtils/gmp_server.py +++ b/DevUtils/gmp_server.py @@ -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): diff --git a/DevUtils/numba_gmpy2_benchmarks.ipynb b/DevUtils/numba_gmpy2_benchmarks.ipynb new file mode 100644 index 0000000..c931f7f --- /dev/null +++ b/DevUtils/numba_gmpy2_benchmarks.ipynb @@ -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 +} diff --git a/DevUtils/requirements.txt b/DevUtils/requirements.txt index 524c166..e5df055 100644 --- a/DevUtils/requirements.txt +++ b/DevUtils/requirements.txt @@ -1,4 +1,5 @@ gmpy2==2.2.0 loguru==0.7.2 +numba==0.60.0 numpy==2.0.0 pytest==8.2.2