Python performance
Python, thanks to its simplicity, has become one of the most used languages in science. However, it has some important drawbacks: let's analyse them!
Python vs C: execution time and memory usage
Python scripting language is well-known for its versatility, simplicity and intuitiveness. However, it has some drawbacks and one of the most relevant is speed: Python is very slow! As you already know, all interpreted languages are slow since they are precompiled in an intermediate form (or not precompiled at all), causing the interpreter to be overloaded with a lot of additional operations. Here I want to show that interpreted languages should be used only when you are sure your program will undertake simple operations only rather than very complex algorithms or iterative/loop calculations over a large amount of data. You will save a lot of time by writing the same code in C and, maybe, also some energy. The planet thanks you!
Here you can find a simple Python2.7 code that finds the highest prime number in a given natural numbers range.
0 1 2 3 4 5 6 7 8 9 10 11 12 13
# Python 2.7 code highestPrime = 0 for i in range(2,100001): flag = 0 for j in range(2,i): if (i%j == 0): flag = 1 break if (flag == 1): continue else: highestPrime = i print highestPrime
Now the C code doing the same thing.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// C code #include <stdio.h> int main(){ int highestPrime = 0; int flag = 0; for(int i=2; i<100001; i++){ flag = 0; for(int j=2; j<i; j++){ if(i%j == 0){ flag = 1; break; } } if(flag == 1){ continue; } else{ highestPrime = i; } } printf("%d\n",highestPrime); return 0; }
Then both of the codes have been run and monitored through 'time': as you can see from the results printed out on my terminal, the time required by the Python2.7 script is way longer than what is needed by the C one. Now, it should be clear that, whenever you have a simple but long set of calculations, it is always good to use a compiled programming language rather than a scripting one.

Solutions for the lazies
You don't want to learn C, I know. Python is so simple and intuitive that you would never abandon it in place of a strange, complex, machine-oriented language like C or similar. So, what can you do? It's plenty of solutions but one of the best choices could be py2exe, a simple tool for Windows. Another solution can be to use a language translator like this one, which works very well: CodeConvert. It is good (obviously you are not going to use it for a 50Mb source code program), it runs pretty smoothly and it gives no problems also for more complex programs. If you find issues with the latter you can always try to fix them by hand but in any case, you'll have the 99% of the job already done.
Another point: though Python is generally slow, Python3 is much faster than Python2.x so don't waste your time, if you want to stay with pure Python, at least switch to the latest release to exploit its new features!
Comments
Please, remember to always be polite and respectful in the comments section. In case of doubts, read this before posting.
Posted comments ⮧
Comment section still empty.
INDEX
INFO


STATISTICS

CONTACTS
SHARE