Work

Benchmarking and Optimization of the Sieve of Eratosthenes in Python and C++

Year: 2025
Type: preprint
Abstract: This paper presents a systematic benchmarking study of the Sieve of Eratosthenes algorithm across Python and C++ implementations. We evaluate eight variants including naive Python, NumPy vectorized, odd-optimized, and memory-efficient versions, alongside C++ implementations with compiler optimizations (-O0 to -O3), parallel processing, and segmented sieves. Our experiments on Intel i5-13420H hardware with input sizes up to 2 × 1 0 7 reveal that optimized C++ achieves 3.67× speedup over unoptimized code, while NumPy Python demonstrates remarkable efficiency (0.00207s for n = 1 0 6 ). The segmented sieve with 12 threads emerges as the most performant (0.0973s for n = 2 × 1 0 7 ), though parallel scaling shows diminishing returns beyond 8 threads. Memory analysis shows C++ implementations maintain stable 5-7MB usage, while Python varies from 0.95MB (NumPy) to 305.18MB (standard list). Key findings indicate: (1) C++ outperforms Python by 1.2-1.7× when both are optimized, (2) NumPy bridges much of this gap with vectorization, and (3) compiler optimizations often surpass parallelization benefits for single-machine execution. The results provide concrete guidance for selecting implementations based on performance, memory, and development constraints.
Language: English
Open Access status: gold
OpenAlex ID: W4414207613
Cites:
Cited by:
Related to: