Quote:
| Originally Posted by ryankask Now I could be dead wrong or am stating my thoughts incorrectly, but aren't the built in functions written in C or if it is Jython, Java? |
Yes, the built in functions are written in C (or Java).
Quote:
| Originally Posted by ryankask The author seems to emphasize speed a lot. I have also read that critics claim Python is too slow. Why not just learn C or Java (even though the learning curve may be a lot higher) if the underlying logic of Python is C code or Java code (is that dead wrong?)? Also, how much "slower" is Python and when would the difference be detectable? (in other words, is this difference completely negligible to the amateur programmer?). I hope this post makes sense...Thank you!
Ryan Kaskel |
There are two sorts of speed. The first is program speed - how fast the code runs. The second is programming speed - how long it took to write. In general, C/C++ (and to a lesser extent Java) are much faster than Python in terms of program speed. However Python is much faster in programming speed.
Sometimes program speed is crucial, but not as often as you think. For programs that process large data files the bulk of the time is often spent reading the data from one file and writing it to another. File IO is slow in any language since it is limited by the physical speed of the disk, so using a faster language may not make a lot of difference. Similarly in GUI programs much of the time the program is idle, waiting for the user to press a key or click the mouse. So long as it can respond fast enough to not piss off the user then it is OK. It does not matter if it takes 10 millisecond or 100 milliseconds to respond as the user is not likely to notice, but a 10 second response would be a pain.
On the other hand programming speed is ALWAYS crucial. Here is an extreme example: imagine you are given a one-off data processing job to do. You could spend 8 hours writing a program in C++ or Java that took an hour to run, or you could spend an hour writing a Python program that took 8 hours to run while you went off and enjoyed yourself. Which would you rather do? Or if you were running a business would you rather have a Python project that took 3 months to deliver a product that was a little sluggish but still usable, or a Java project that took a year to deliver a product that was a bit faster (maybe)? Thats a difference of 9 months revenue.
Even in cases where program speed is important, most of the time is usually spent in a few bottlenecks in the code. If you can write the program quickly in Python then you have more time to spend optimising those hotspots, either by changing the algorithms, fine tuning the Python code, or recoding key functions in C/C++/Java. This explains the apparent paradox that while statement-for-statement Python is significantly slower than C++ or Java*, Python
programs are often as fast or even faster than their compiled counterpart. There is also a Python compiler (
psyco) that can significantly speed up Python code.
In summary, don't worry about speed until you really have to.
Dave - The Developers' Coach
* Actually the speed difference between Java and Python is not as big as you might expect. Both are compiled into an intermediate bytecode which is then interpreted. Java has an advantage in that it is statically typed so does not have to do dynamic lookups for every function call. Python has an advantage that some of the bytecodes are at a higher level so do more work for each instruction. These can cancel out to some extent.