Abstract
This study presents a comparative analysis of a traditional, pure Python-based approach and a modern, JAX library-based approach for calculating the harmonic series, focusing on performance, scalability, and efficiency. The harmonic series is a divergent series of significant importance in mathematical analysis. In computational mathematics, the effective evaluation of such series is critical, particularly in applications requiring large-scale data and high precision. The traditional pure Python approach utilises the fractions.Fraction class to provide exact arithmetic, enhancing the code's readability and accuracy. However, this method exhibits linear time complexity for large values of n, limiting its performance in the context of repetitive calculations. Furthermore, memory usage and processing time increase linearly with n. In contrast, the JAX-based approach (the Oresmej module) leverages the advanced features of JAX, a library designed for high-performance scientific computing, including vectorisation (vmap), just-in-time (JIT) compilation, and automatic differentiation. This approach yields a significant speed advantage, especially in large-scale and repetitive operations. Additionally, caching is supported via @lru_cache, which prevents re-computation for identical inputs. The return value is defined as a tuple, thereby enhancing data integrity and cache compatibility. The comparative analysis was conducted using micro-benchmarking techniques. Following a warm-up period, the JAX-based approach was found to be several hundred times faster than pure Python, with cached functions producing results almost instantaneously. Therefore, for scientific research demanding high performance, scalability, and sustainable computation, approaches based on Cython (Cythonize), Numba, JAX, NumPy, and parallel processing (multiprocessing, joblib) are strongly recommended.
Supplementary Material
File (from loops to vectorisation a performance comparison of pure python and jax for harmonic series calculation.pdf)
- Download
- 825.37 KB
Information & Authors
Information
Version history
Copyright
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
Keywords
- 2025
- caching
- cython
- cythonize
- divergent series. from loops to vectorisation: a performance comparison of pure python and jax for harmonic series calculation
- exact arithmetic
- green coding
- harmonic series
- jax
- jit compilation
- joblib
- mehmet keçeci
- multiprocessing
- numba
- numpy
- oresme
- oresmej
- performance comparison
- pure python
- scientific computing
- sustainable coding
- vectorisation
- yeşil kodlama
Authors
Metrics & Citations
Metrics
Article Usage
257views
126downloads
Citations
Download citation
Mehmet Keçeci.
From Loops to Vectorisation: A Performance Comparison of Pure Python and JAX for Harmonic Series Calculation, Mehmet Keçeci, 2025. Authorea. 30 July 2025.
DOI: https://doi.org/10.22541/au.175390610.08488249/v1
DOI: https://doi.org/10.22541/au.175390610.08488249/v1
If you have the appropriate software installed, you can download article citation data to the citation manager of your choice. Simply select your manager software from the list below and click Download.
For more information or tips please see 'Downloading to a citation manager' in the Help menu.