Python Can Be Fast Than C++
Tags: Python, Problem Solving, Software Development, Programming, Cplusplus

Python Can Be Fast Than C++

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

http://narrabri.net/fub/Sch-v-Fre-dfb-01.html

http://narrabri.net/fub/Sch-v-Fre-dfb-02.html

http://narrabri.net/fub/Sch-v-Fre-dfb-03.html

http://narrabri.net/fub/Sch-v-Fre-dfb-04.html

http://narrabri.net/fub/Sch-v-Fre-dfb-05.html

http://narrabri.net/fub/Juv-v-Ata-notizie-01.html

http://narrabri.net/fub/Juv-v-Ata-notizie-02.html

http://narrabri.net/fub/Juv-v-Ata-notizie-03.html

http://narrabri.net/fub/Juv-v-Ata-notizie-04.html

http://narrabri.net/fub/Juv-v-Ata-notizie-05.html

http://narrabri.net/fub/v-ideo-Nantes-foot-fr-01.html

http://narrabri.net/fub/v-ideo-Nantes-foot-fr-02.html

http://narrabri.net/fub/v-ideo-Nantes-foot-fr-03.html

http://narrabri.net/fub/Nm-v-Nc-direct-tv-01.html

http://narrabri.net/fub/Nm-v-Nc-direct-tv-02.html

http://narrabri.net/fub/Nm-v-Nc-direct-tv-03.html

http://narrabri.net/fub/Mon-v-Met-sncf-01.html

http://narrabri.net/fub/Mon-v-Met-sncf-02.html

http://narrabri.net/fub/Mon-v-Met-sncf-03.html

http://narrabri.net/fub/Dijon-v-Lille-chaine-tv1.html

http://narrabri.net/fub/Dijon-v-Lille-chaine-tv2.html

http://narrabri.net/fub/Dijon-v-Lille-chaine-tv3.html

http://narrabri.net/fub/Ang-v-Stb-foot-fr-01.html

http://narrabri.net/fub/Ang-v-Stb-foot-fr-02.html

http://narrabri.net/fub/Ang-v-Stb-foot-fr-03.html

https://aicbelize.azurewebsites.net/fcb/Sch-v-Fre-dfb-01.html

https://aicbelize.azurewebsites.net/fcb/Sch-v-Fre-dfb-02.html

https://aicbelize.azurewebsites.net/fcb/Sch-v-Fre-dfb-03.html

https://aicbelize.azurewebsites.net/fcb/Sch-v-Fre-dfb-04.html

https://aicbelize.azurewebsites.net/fcb/Sch-v-Fre-dfb-05.html

https://aicbelize.azurewebsites.net/fcb/Juv-v-Ata-notizie-01.html

https://aicbelize.azurewebsites.net/fcb/Juv-v-Ata-notizie-02.html

https://aicbelize.azurewebsites.net/fcb/Juv-v-Ata-notizie-03.html

https://aicbelize.azurewebsites.net/fcb/Juv-v-Ata-notizie-04.html

https://aicbelize.azurewebsites.net/fcb/Juv-v-Ata-notizie-05.html

https://aicbelize.azurewebsites.net/fcb/v-ideo-Nantes-foot-fr-01.html

https://aicbelize.azurewebsites.net/fcb/v-ideo-Nantes-foot-fr-02.html

https://aicbelize.azurewebsites.net/fcb/v-ideo-Nantes-foot-fr-03.html

https://aicbelize.azurewebsites.net/fcb/Nm-v-Nc-direct-tv-01.html

https://aicbelize.azurewebsites.net/fcb/Nm-v-Nc-direct-tv-02.html

https://aicbelize.azurewebsites.net/fcb/Nm-v-Nc-direct-tv-03.html

https://aicbelize.azurewebsites.net/fcb/Mon-v-Met-sncf-01.html

https://aicbelize.azurewebsites.net/fcb/Mon-v-Met-sncf-02.html

https://aicbelize.azurewebsites.net/fcb/Mon-v-Met-sncf-03.html

https://aicbelize.azurewebsites.net/fcb/Dijon-v-Lille-chaine-tv1.html

https://aicbelize.azurewebsites.net/fcb/Dijon-v-Lille-chaine-tv2.html

https://aicbelize.azurewebsites.net/fcb/Dijon-v-Lille-chaine-tv3.html

https://aicbelize.azurewebsites.net/fcb/Ang-v-Stb-foot-fr-01.html

https://aicbelize.azurewebsites.net/fcb/Ang-v-Stb-foot-fr-02.html

https://aicbelize.azurewebsites.net/fcb/Ang-v-Stb-foot-fr-03.html

https://aicbelize.azurewebsites.net/serv0/Video-Schalke-v-Freiburg-hq01.html

https://aicbelize.azurewebsites.net/serv0/Video-Schalke-v-Freiburg-hq02.html

https://aicbelize.azurewebsites.net/serv0/Video-Schalke-v-Freiburg-hq03.html

https://aicbelize.azurewebsites.net/serv0/Epl-Arsenal-v-Southampton-v-en01.html

https://aicbelize.azurewebsites.net/serv0/Epl-Arsenal-v-Southampton-v-en02.html

https://aicbelize.azurewebsites.net/serv0/Epl-Arsenal-v-Southampton-v-en03.html

https://aicbelize.azurewebsites.net/serv0/videos-Leeds-v-Newcastle-lez-01.html

https://aicbelize.azurewebsites.net/serv0/videos-Leeds-v-Newcastle-lez-02.html

https://aicbelize.azurewebsites.net/serv0/videos-Leeds-v-Newcastle-lez-03.html

https://aicbelize.azurewebsites.net/serv0/Video-Leicester-v-Everton-en-gb01.html

https://aicbelize.azurewebsites.net/serv0/Video-Leicester-v-Everton-en-gb02.html

https://aicbelize.azurewebsites.net/serv0/Video-Leicester-v-Everton-en-gb03.html

https://aicbelize.azurewebsites.net/serv0/video-Juve-v-Ath-diretta01.html

https://aicbelize.azurewebsites.net/serv0/video-Juve-v-Ath-diretta02.html

https://aicbelize.azurewebsites.net/serv0/video-Juve-v-Ath-diretta03.html

https://aicbelize.azurewebsites.net/serv0/video-Juve-v-Ath-diretta04.html

https://aicbelize.azurewebsites.net/serv0/video-Juve-v-Ath-diretta05.html

https://aicbelize.azurewebsites.net/serv0/video-County-v-Swansea-City-01.html

https://aicbelize.azurewebsites.net/serv0/video-County-v-Swansea-City-02.html

https://aicbelize.azurewebsites.net/serv0/video-County-v-Swansea-City-03.html

https://aicbelize.azurewebsites.net/serv0/video-County-v-Swansea-City-04.html

https://aicbelize.azurewebsites.net/serv0/video-County-v-Swansea-City-05.html

https://aicbelize.azurewebsites.net/serv0/Video-Antwerp-v-Zulte-hd01.html

https://aicbelize.azurewebsites.net/serv0/Video-Antwerp-v-Zulte-hd02.html

https://aicbelize.azurewebsites.net/serv0/Video-Antwerp-v-Zulte-hd03.html

https://aicbelize.azurewebsites.net/serv0/Video-Antwerp-v-Zulte-hd04.html

https://aicbelize.azurewebsites.net/serv0/Video-Antwerp-v-Zulte-hd05.html

http://narrabri.net/una/Video-Schalke-v-Freiburg-hq01.html

http://narrabri.net/una/Video-Schalke-v-Freiburg-hq02.html

http://narrabri.net/una/Video-Schalke-v-Freiburg-hq03.html

http://narrabri.net/una/Epl-Arsenal-v-Southampton-v-en01.html

http://narrabri.net/una/Epl-Arsenal-v-Southampton-v-en02.html

http://narrabri.net/una/Epl-Arsenal-v-Southampton-v-en03.html

http://narrabri.net/una/videos-Leeds-v-Newcastle-lez-01.html

http://narrabri.net/una/videos-Leeds-v-Newcastle-lez-02.html

http://narrabri.net/una/videos-Leeds-v-Newcastle-lez-03.html

http://narrabri.net/una/Video-Leicester-v-Everton-en-gb01.html

http://narrabri.net/una/Video-Leicester-v-Everton-en-gb02.html

http://narrabri.net/una/Video-Leicester-v-Everton-en-gb03.html

http://narrabri.net/una/video-Juve-v-Ath-diretta01.html

http://narrabri.net/una/video-Juve-v-Ath-diretta02.html

http://narrabri.net/una/video-Juve-v-Ath-diretta03.html

http://narrabri.net/una/video-Juve-v-Ath-diretta04.html

http://narrabri.net/una/video-Juve-v-Ath-diretta05.html

http://narrabri.net/una/video-County-v-Swansea-City-01.html

http://narrabri.net/una/video-County-v-Swansea-City-02.html

http://narrabri.net/una/video-County-v-Swansea-City-03.html

http://narrabri.net/una/video-County-v-Swansea-City-04.html

http://narrabri.net/una/video-County-v-Swansea-City-05.html

http://narrabri.net/una/Video-Antwerp-v-Zulte-hd01.html

http://narrabri.net/una/Video-Antwerp-v-Zulte-hd02.html

http://narrabri.net/una/Video-Antwerp-v-Zulte-hd03.html

http://narrabri.net/una/Video-Antwerp-v-Zulte-hd04.html

http://narrabri.net/una/Video-Antwerp-v-Zulte-hd05.html

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

: 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