Runtime comparison of different implementations of calculating Prime numbers.
- Download the newest version of PyPy (skip if you're not planning on using PyPy).
- Download the newest version of Anaconda (skip if you're not planning on using Anaconda).
- Download or clone the repository.
- Install the pip requirements with
pip install -r requirements.txt -U
- Run the program! I included the related
.bat
and.sh
files for running the different runtimes as needed.
- Windows:
- Clean.bat: Deletes all compiled Python files
(
*.c
,*.pyc
,*.pyd
,*.so
) including the__pycache__
folder, and delete any text files including thefiles_runs
andfiles_compile
folders. - Compile_Python.py: Runs Clean.bat, then
recompiles all
*.py
files using thepy_compile
module, then uses Cython to compile all*.pyx
files. - Compile_Conda.py: Same as Compile_Python.py, but uses the Anaconda runtime. This will ask you to create an environment, which will use Python 3.7 and install cython and numpy automatically.
- Clean.bat: Deletes all compiled Python files
(
Test | Description |
---|---|
Primes | Find every Prime Number up to a given integer |
Fibonacci | Calculate all Fibonacci numbers up to a given integer |
Database Usage | Run different database operations |
Image Processing | Apply modifications to images |
Video Processing | Apply modifications to videos |
- Python Runtimes:
- CPython: The standard C-based Python runtime
- Pypy: An alternative implementation of the CPython runtime, utilizing a JIT compiler, but lacks support for some CPython extensions
- Cinder: Meta's internal CPython implementation, highly unsupported for external use cases
- Pyston: Fork of the CPython 3.8 runtime with a claimed 30% performance improvement
- Compilation:
- Interpreted: All code is interpreted by the Python runtime
- Compiled Cython: Base code is compiled with Cython without any Cython optimizations
- Optimized Cython: Base code is compiled with Cython and is modified to utilize Cython optimizations (such as static typing)
- Numba JIT: Base code is compiled just-in-time with Numba
- Numba AOT: Base code is compiled ahead-of-time with Numba
- Code Branching:
- Inline / Imperative: Code is run within the main function
- Function: Code is put into it's own function
- Caching:
- Least-Recently-Used Cache: In-memory caching with Python built-ins
functools.lru_cache
- JSON Cache: File-based caching with JSON files
- SQLite Cache: Database caching with local SQLite
- Redis Cache: Database caching with in-memory Redis service
- Least-Recently-Used Cache: In-memory caching with Python built-ins
- Data Storage Objects:
- Lists: Store results inside mutable Python lists
- Tuples: Store results inside immutable Python tuples
- Numpy Arrays: Store results inside mutable in-memory Numpy arrays
- Pandas DataFrames / Series: Store results inside mutable Pandas DataFrames / Series
- Data Comprehension:
- For Loops: The "standard" method of iterating over data.
- List Comprehension: Create a mutable list directly from generators
- Tuple Comprehension: Create an immutable tuple directly from generators
- Numpy Array Methods: Apply functions to Numpy arrays
- Pandas DataFrame / Series Methods: Apply functions to Pandas DataFrames / Series
- Miscellaneous Python Tricks:
- Generators: Yield results as they come, rather than return an entire container of the results
- Lambdas: Anonymous functions rather than defined named ones
- Maps: Apply a function over an iterable rather than handle iterating manually
For some given number N
, find every number below N
Since this is a mathematical test, we can have some shortcuts.
For prime numbers, we already know the following:
- All prime numbers are odd numbers, except for 2.
- We only need to start with 2 as the first prime number, then loop from 3
to
N
, incrementing by 2 for each iteration to test only odd numbers. - Instead of checking every number up to
N
we can stop atfloor(N/2)
- Instead of checking every number up to
floor(N/2)
we can stop atfloor(math.sqrt(N))
- Instead of doing pure math, we can use bitshifting.
- Compiler_Cython.py: Used by Cython to compile the
- Compiler_Cython_Lambda.py: Used by Cython to compile the
- Threader.py:
- Python wrapper for calling the programs.
- It's handles the multiprocessing/threading of the other programs, which involves creating a process for each one of the different tests and running those tests simultaneously.
- Each program's runtime is measured, and is both displayed to the user
inside the console as well as saved to files under the
files_runs
folder.