Is Python Slow? Python is a programming language that I learnt somewhat recently something like 2, 3 years ago and that I like very much. It is simple, to the point, and has several functional -like constructs that I am already familiar with. But Python is slow compared to other programming languages. But it was unclear to me just how slow Python was compared to other languages. It just felt slow.
|Country:||Sao Tome and Principe|
|Published (Last):||12 April 2010|
|PDF File Size:||4.78 Mb|
|ePub File Size:||15.61 Mb|
|Price:||Free* [*Free Regsitration Required]|
Is Python Slow? Python is a programming language that I learnt somewhat recently something like 2, 3 years ago and that I like very much.
It is simple, to the point, and has several functional -like constructs that I am already familiar with. But Python is slow compared to other programming languages. But it was unclear to me just how slow Python was compared to other languages. It just felt slow. So I have decided to investigate by comparing the implementation of a simple, compute-bound problem, the eight queens puzzle generalized to any board dimensions. This puzzle is most easily solved using, as Dijkstra did, a depth-first backtracking program, using bitmaps to test rapidly whether or not a square is free of attack 1.
I then compared the resulting speeds. So this means that no queen is on the same row, column, or diagonal as another queen. Using a real chessboard and pawns in lieu of queens, you can easily find a solution in a few seconds. Now, to make things more interesting, we might be interested in enumerating all solutions and, for the time being, neglecting to check if a solution is a rotated or mirrored version of another solution. The basic algorithm to solve the Eight queens puzzle is a relatively simple recursive algorithm.
First, we place a queen somewhere on the first row and we mark the row it occupies as menaced. We also do so with the column and two diagonals. We then try to place a second queen somewhere on the second row, on a square that is menace-free, and mark the row, column, and diagonals of the new queen as menaced.
And we proceed in the same fashion for other queens. But suppose that at the th stage, we cannot find a menace-free square, preventing us from placing the th queen. If the situation arises, we give up for the th queen and backtrack to the th queen.
We try a new and never tried before location for the th queen and we go forth trying to place the th queen. If may happen that we go all the way back to the th queen because there are no other solutions for the th queen, which asks for the th queen to be moved, which can also result in a dead-end; and so forth all the way down to the th queen. Because the algorithm proceeds depth-first and can rear back quite a bit in looking for new solutions, it is called a backtracking algorithm. Backtracking algorithms are key to many artificial intelligence systems like, well, chess programs.
And just for kicks, I decided to port it to different languages, with help from friends for Java and C of which I know about zilch. The Python versions, as there are two of them also, differ on how python-esque they are. The Bash version is necessarily rather bash-esque as Bash does not offer anything much more sophisticated than arrays in terms of data structures.
All implementations were compiled with all optimizations enabled -O3, inlining, interprocedural optimizations, etc. At first, the results are not very informative: Results, Time, Linear Scale As expected, all times shoot up quite fast, with, unsurprisingly, BASH shooting up faster, followed by the two Python implementations. We now get: We can use a log-scale for the axis to better separate the similar results: Speed Ratio, Log Scale We see three very strange things. The first is Bash shooting up wildly.
The second is that C relative time goes down with the increasing board size. The third is that before , the results are fluctuating. The first is easily explained: Bash is incredibly slow. It is so slow that the two last data points are estimated as it would have taken an extra week, or so, to get them. The second anomaly needs a bit more analysis. Looking at the raw timing data, we can see that the C version seems to be needing an extra 7ms regardless of board size.
I do not know where that comes from, as the timings do not time things such as program load and initialization, but only the solving of the puzzle itself. It may well be the JIT that takes a while to figure out that the recursive function is expensive and the run-time compilation takes 7ms? Anyway, would we remove this mysterious extra 7ms, the odd behaviour of the C implementation would vanish. Suffice to move the mouse and generate a couple of interrupts to throw timing off considerably.
Removing the small board sizes yields: Speed Ratios, Log Scale, Without Outliers which shows that the respective implementations are well behaved except for C and its most probably JIT-related extra 7ms. The Java version takes about 1. For one thing, that Bash is slow. From what I understand from Bash, data structures are limited to strings and arrays. Lists and strings are the same. Basically, a list is merely a string with items separated by the IFS character s , which causes all array-like accesses to lists to be performed in linear time as each time the string is reinterpreted given the current IFS.
I do not think that bash uses something like a virtual machine and an internal tokenizer to speed up script interpretation. In any case, I also learnt that Bash is a lot more generic than I thought. The other thing is that Python is not a programming language for compute-bound problems. This makes me question how far projects such as Pygame which aims at developing a cromulent gaming framework for Python can go.
While all of the graphics and sound processing can be off-loaded to third party libraries written in C or assembly language and interacting with the platform-specific drivers, the central problem of driving the game itself remains complete. What about strategy games? Could you write a championship-grade chess engine in Python? I wish for Python 4 to have a new virtual machine and interpreter to bring Python back with Java and C , performance-wise. Better yet, why not have a true native compiler like gcj for Python?
But its extreme simplicity and its somewhat universal backtracking structure makes it an especially adequate toy problem. All times are in seconds.
Preview this item Preview this item. Publication: Der Schockwellenreiter You may have already requested this item. At this point Nick becomes determined to escape. These become manifest in schlckwellenreiter of his abilities, revealing his identity to his pursuers.
How to convert PDF files to ePub files to read on your iPad with iBooks