Thread: Why Python?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14

    Question Why Python?


    I am presently reading the book "Learning Python" (2nd ed.) which I am sure many of you have read. I really like Python's syntax and clarity but several points the author makes cause me to sometimes question why use Python as opposed to a fully compiled language like Java or C. For example, I just finished reading the section about lambdas and other 'advanced functions.' The author says that these functions run much faster as opposed to 'hardcoding' their purpose with for loops, etc. 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? 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
    Last edited by ryankask; June 10th, 2004 at 03:48 PM. Reason: some spelling
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14
    To extend this question, because I realize it applies to other popular languages like PHP, etc., when does code stop being compiled or interpreted. Also, if you have language and it is compiled, what language is the compiler written in and what "understands" the compiler and then what "understands" what "understands" the compiler, and so on. When does a lanauge finally become understood by electronics...maybe someone catches my drift. Are there any book on understanding something like this? I did see one published by Microsoft (hard cover) which looked interesting...thanks.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    6
    Rep Power
    0
    Originally Posted by ryankask
    ... I did see one published by Microsoft (hard cover) which looked interesting...thanks.
    Is the book you're referring to "Code"? Whether or not it is, I'd highly recommend it. It gives a very understandable and readable look at some of the lower-level events occurring in your computer. It might not explain exactly what you're asking, but it will probably help you better understand whatever answers you get.

    It's been a while since I read it, but I don't remember any m$ propaganda in it. I remember that being my biggest concern when I bought it, but I was pleasantly surprised.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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).


    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    52
    Rep Power
    14
    Thank you Dave for that great explanation!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    15
    Rep Power
    0
    Originally Posted by DevCoach
    * 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.
    Partially because of this comparisons are very slow in Python and should be avoided. Use built-in sorting on complex data by applying the Schwartzian transform or somthing similar.

IMN logo majestic logo threadwatch logo seochat tools logo