Python Can Be Faster Than C++
A trick that makes Python faster more than you can imagine
Python is a great versatile programming language. Even though python is used most for machine learning problem solving because of its library and high-level language, it is known to be slower than many other languages. Because of its reputation, many would decide to leave the language behind and stick with other options like C++ for program solving.
In this article, I will show you how Python is faster than C++.
Basic Speed Testing
To test the normal speed difference between Python and C++, I will test the execution time of the Generation of primes algorithm. It is a simple algorithm with clear short steps in both languages.
Generation of primes flowchart
Python Implementation
import math
from time import per_counter def is_prime(num):
if num == 2:
return True;
if num <= 1 or not num % 2:
return False
for div in range(3,int(math.sqrt(num)+1),2):
if not num % div:
return False
return Truedef run program(N):
for i in range(N):
is_prime(i)if __name__ == ‘__main__’:
N = 10000000
start = perf_counter()
run_program(N)
end = perf_counter()
print (end — start)
C++ Implementation
#include <iostream>
#include <cmath>
#include <time.h>using namespace std;bool isPrime(int num)
{
if (num == 2) return true;
if (num <= 1 || num % 2 == 0) return false;
double sqrt_num = sqrt(double(num));
for (int div = 3; div <= sqrt_num; div +=2){
if (num % div == 0) return false;
}
return true;}int main()
{
int N = 10000000;
clock_t start,end;
start = clock();
for (int i; i < N; i++) isPrime(i);
end = clock();
cout << (end — start) / ((double) CLOCKS_PER_SEC);
return 0;}
Results
- Python: 80.137 seconds
- C++ : 3.174 seconds
Comment
As expected, C++ was 25 times faster than Python in our basic test. So the reputation is true, and this is logical as
- Python is a dynamic language, unlike C++.
- GIL (Python Global Interpreter) doesn’t allow parallel programming.
This speed difference can be fixed as python was created to be a flexible versatile language. One of the top solutions for the speed problem is using Numba.
Numba
Numba is an open source JIT compiler that translates a subset of Python and NumPy code into fast machine code. ~ Wikipedia
Briefly, Numba is a library that makes Python fast. It is very easy to use and dramatically change how fast your code. To start using Numba, just install it using the console use
pip install numba
Python Implementation After Using Numba
import math
from time import per_counter
from numba import njit, prange@njit(fastmath=True, cache=True)
def is_prime(num):
if num == 2:
return True;
if num <= 1 or not num % 2:
return False
for div in range(3,int(math.sqrt(num)+1),2):
if not num % div:
return False
return True@njit(fastmath=True, cache=True,parallel=True)
def run program(N):
for i in prange(N):
is_prime(i)if __name__ == ‘__main__’:
N = 10000000
start = perf_counter()
run_program(N)
end = perf_counter()
print (end — start)
Results
Python: 1.401 seconds
C++ : 3.174 seconds
So Python is faster than C++. So it is possible to speed up your algorithms in Python to be faster than C++.