why python is slower than java?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maurice LING

    why python is slower than java?

    This may be a dumb thing to ask, but besides the penalty for dynamic
    typing, is there any other real reasons that Python is slower than Java?

    maurice
  • Bryan

    #2
    Re: why python is slower than java?

    Maurice LING wrote:[color=blue]
    > This may be a dumb thing to ask, but besides the penalty for dynamic
    > typing, is there any other real reasons that Python is slower than Java?
    >
    > maurice[/color]

    at work, we use java and python. we have projects using swing and others using wxpython. we have applications that do
    intensive io and others that do intensive cpu. we have not found that python is slower than java. in fact, when it
    comes to gui's, our swing apps take "forever" to startup and when when garbage collector starts, the whole app just
    freezes for about 15 seconds. our wxpython apps, start right up and "feel" faster and snappier. can you show an
    example of where python's "slow" speed as compared to java's "fast" speed has negatively impacted your application or
    has been noticable in any way? i know this is a trolling question you have posted, but i'm actually very interested
    knowing why you have said this.

    thanks,

    bryan

    Comment

    • Maurice LING

      #3
      Re: why python is slower than java?

      [color=blue]
      > at work, we use java and python. we have projects using swing and
      > others using wxpython. we have applications that do intensive io and
      > others that do intensive cpu. we have not found that python is slower
      > than java. in fact, when it comes to gui's, our swing apps take
      > "forever" to startup and when when garbage collector starts, the whole
      > app just freezes for about 15 seconds. our wxpython apps, start right
      > up and "feel" faster and snappier. can you show an example of where
      > python's "slow" speed as compared to java's "fast" speed has negatively
      > impacted your application or has been noticable in any way? i know this
      > is a trolling question you have posted, but i'm actually very interested
      > knowing why you have said this.
      >
      > thanks,
      >
      > bryan[/color]

      Thanks, lets just say that I have no interest in trolling.

      1st of all, I thought it is somehow common knowledge that python is
      slower than java in many cases. Though I may be wrong... When I do a
      Google search, this came up...



      although http://page.mi.fu-berlin.de/~prechel.../jccpprtTR.pdf
      showsjust the opposite.

      What I need to work on now is something that requires speed (and dealing
      with files), without user's intervention. So the part about users' delay
      time is not in the equation. My choices boils down to Python or Java.

      Cheers
      maurice

      Comment

      • Bryan

        #4
        Re: why python is slower than java?

        Maurice LING wrote:[color=blue]
        >[color=green]
        >> at work, we use java and python. we have projects using swing and
        >> others using wxpython. we have applications that do intensive io and
        >> others that do intensive cpu. we have not found that python is slower
        >> than java. in fact, when it comes to gui's, our swing apps take
        >> "forever" to startup and when when garbage collector starts, the whole
        >> app just freezes for about 15 seconds. our wxpython apps, start right
        >> up and "feel" faster and snappier. can you show an example of where
        >> python's "slow" speed as compared to java's "fast" speed has
        >> negatively impacted your application or has been noticable in any
        >> way? i know this is a trolling question you have posted, but i'm
        >> actually very interested knowing why you have said this.
        >>
        >> thanks,
        >>
        >> bryan[/color]
        >
        >
        > Thanks, lets just say that I have no interest in trolling.
        >
        > 1st of all, I thought it is somehow common knowledge that python is
        > slower than java in many cases. Though I may be wrong... When I do a
        > Google search, this came up...
        >
        > http://twistedmatrix.com/users/glyph...n-vs-java.html
        >
        > although http://page.mi.fu-berlin.de/~prechel.../jccpprtTR.pdf
        > showsjust the opposite.
        >
        > What I need to work on now is something that requires speed (and dealing
        > with files), without user's intervention. So the part about users' delay
        > time is not in the equation. My choices boils down to Python or Java.
        >
        > Cheers
        > maurice[/color]

        but you aren't saying exactly _what_ requires speed and exactly what your requirements for the project is. you are being
        way too general here for anyone to really help you. as for working with files, i much rather do that in python than
        java. i may be wrong, but i thought java's file IO was pretty much a thin wrapper over c. so i don't think you will
        have any speed problems with python's file IO compared to java's. also, just for fun, write the following fully
        working python program in java:

        import time
        t = time.time()
        s = open('in.txt'). read()
        open('out.txt', 'w').write(s)
        print time.time() - t

        (by the way...i was able to write the above lines of python code without referring to a manual :)

        if it's not too much trouble, could you please post your code here along with the time results for the above code and
        your java code? you can test a small file and a large one.

        thanks,

        bryan

        Comment

        • Christopher Koppler

          #5
          Re: why python is slower than java?

          On Fri, 05 Nov 2004 07:11:33 +0000, Maurice LING wrote:
          [color=blue]
          > What I need to work on now is something that requires speed (and dealing
          > with files), without user's intervention. So the part about users' delay
          > time is not in the equation. My choices boils down to Python or Java.[/color]

          Speed in what area? Dealing with files, is it a heavily I/O-based program?
          If so, choice of programming language will very probably hardly impact
          performance (well, depending of course on exactly you want to do, there
          may of course be differences...) .
          Or do you want to crunch numbers? Python has great extensions for that,
          written in highly optimized C. Come to think of it, Python has great
          built-in datatypes and extensions for all manner of things, written in
          highly optimized C. So even if your application wants to do some heavy
          processing of <whatever> that doesn't depend on external factors like I/O
          speed, the network or user interaction, Python will generally be fast
          enough.
          And as for the statically typed nature of Java providing speed benefits,
          take a look at Psyco, which often (but not always) helps enormously.

          So everything boils down to two points: what exactly you want to do - if
          you can tell us that, someone better qualified and less biased than
          myself might actually be able to offer you concrete advice on what to
          choose - and as always: premature optimization is the root of all evil :-)

          --
          Christopher

          Comment

          • John Doe

            #6
            Re: why python is slower than java?

            On Fri, 05 Nov 2004 05:58:09 +0000, Maurice LING wrote:
            [color=blue]
            > This may be a dumb thing to ask, but besides the penalty for dynamic
            > typing, is there any other real reasons that Python is slower than Java?
            >
            > maurice[/color]

            Hi Maurice,
            the issue is compiling and bytecode. Python and Java compile source to
            their own bytecodes. Bytecode is a binary that is portable between many
            Operating Systems. Some time in 1999 (I think) IBM developed a jit or
            Just In Time compiler for Java (Apple invented the JIT concept earlier to
            make 68000 code work on their new line of PowerPC Macs).

            The JIT compiler compiled java to both bytecode and native OS opcode. This
            offered increase speed on the second + runs. The binary was suppose to be
            compiled into a block containing bytecode and opcode. If the java runtime
            noted that the opcode segment was 'native', it was suppose to run it, over
            the bytecode. This gives the 'faster' perception.

            Of course some company, afraid their developers might build there GUI apps
            with java, over their native mode (thus making their application work
            anywhere Java worked) went ahead and developed a version of the java
            compiler that stripped out the bytecode. This made the binary
            smaller, but locked it to one OS/arch. You can guess about which
            corporation that was. Lawsuits followed. The company in question lost and
            decided to drop Java for their own version of Java, JavaLite, sometimes
            called 'see' 'sharp'.

            Now as to speed... If you want speed, develop in Python or Perl to
            get the form of your program. Development time is faster, even if the
            execution is slow. Both provide OOP bindings. Once the program is up and
            running, you can use profiling to determine which parts of your
            interpreted code is slowest and most often used. These you rewrite as
            C/C++ code and call them from within python.

            In this way version 1.0 is all python. v1.1 has 20% C++; v1.2 is 34% C++
            .... until v2.0 is all C++. Each upgrades is faster, but does not introduce
            any new functionality and the risks that new functions imply.

            If you were faithful to this concept, version 3 requires you to use the
            oldest python branch who's functions will not be modified, to start
            development. Could be v1.1 or 1.9.

            C/C++ is for speed, not development.

            Dan Atterton
            atterdan@yahoo. com



            Comment

            • Israel Raj T

              #7
              Re: why python is slower than java?

              Maurice LING <mauriceling@ac m.org> writes:
              [color=blue]
              > This may be a dumb thing to ask, but besides the penalty for dynamic
              > typing, is there any other real reasons that Python is slower than
              > Java?[/color]

              Do you have any data to support this ?
              On Windows XP, Windows NT and Linux (Gentoo ), I have consistently
              found python apps using wxpython to be far faster to load
              and to be far more responsive than Java apps.

              Comment

              • Israel Raj T

                #8
                Re: why python is slower than java?





                On the CPU score, Java scores 28.4 while Python is within cooee at 27.
                (bigger is better )

                On the updated version at http://shootout.alioth.debian.org/craps.php,
                Python beats at least two java implementations .

                Comment

                • EP

                  #9
                  Re: why python is slower than java?

                  John Doe wrote (posthumously?) :
                  [color=blue]
                  > In this way version 1.0 is all python. v1.1 has 20% C++; v1.2 is 34%
                  > C++
                  > ... until v2.0 is all C++. Each upgrades is faster, but does not
                  > introduce
                  > any new functionality and the risks that new functions imply.
                  >
                  > If you were faithful to this concept, version 3 requires you to use the
                  > oldest python branch who's functions will not be modified, to start
                  > development. Could be v1.1 or 1.9.
                  >
                  > C/C++ is for speed, not development.[/color]


                  Am I the only one to think this works, but makes no sense?

                  If one is to migrate their Python code to C++, one ends up with C++ code, which is great unless there is that (not so very rare) event where one wantsto add new features, address new requirements, etc., etc. Applications need to evolve. If the finished code is going to be C++, one might just as well program in C++ from the start instead of aportioning mindshare between the two languages. Mindshare devoted to thinking in a second language might instead be devoted to thinking about the application.

                  Python seems to exemplify living code and since good applications have a life of their own, shouldn't the code live as well?

                  Dipping into C++ for program hotspots seems like good pramaticism, but at the expense of the simplicity and clarity that is otherwise Pythonic.

                  Fast Python is an answer, a patchwork of programming languages is a work around.


                  Eric

                  I won't say that Python needs to be faster, but imagine Python running as fast as C++ without any special tricks and you can envision the opportunities lost due to Python lacking blazing speed.

                  Comment

                  • Ian Bicking

                    #10
                    Re: why python is slower than java?

                    Maurice LING wrote:[color=blue]
                    > This may be a dumb thing to ask, but besides the penalty for dynamic
                    > typing, is there any other real reasons that Python is slower than Java?[/color]

                    Python is generally late-bound, which means much more happens at runtime
                    compared to Java. For instance, consider accessing an attribute,
                    "self.x". In Java you know what the class is shaped like, exactly.
                    Usually this turns into some bytecode like "access the variable of self,
                    at offset 3". I don't know any of the details, but that's the general gist.

                    Similarly, there's many other optimizations where you can know at
                    compile time exactly what functions are called, how variables are
                    stored, etc. Using that information, you can do optimizations like
                    inline code. In the same places in Python, you are typically doing one
                    (or several!) hashtable lookups based on the variable or attribute name.

                    But even in Java there are limits. Because of interfaces and subclasses
                    you can know the shape of self/this, but you can't know the exact shape
                    of the objects around you (C++ is more aggressive in this respect, and
                    often can determine the exact shape; but in the process it's dangerous
                    to put together different pieces of compiled code when they don't know
                    about each other, which is why binary interfaces there are fragile).
                    Anyway, Java does a lot of the same stuff as Python when the exact type
                    is determined at runtime (which is frequently). This is where JIT comes
                    in, doing the optimizations at runtime; it is still limited, as it
                    cannot guarantee the type of the objects in the system, but must check
                    them each time before using the optimized path. And, actually, Python
                    can do the same sort of things with psyco. It's still harder in Python,
                    and the end result not as effective, but it's one among many tools.

                    If Java wasn't doing any optimizations, I don't think it would be
                    significantly faster than Python.

                    Also note that Python assimilates external (C, Fortan, etc) libraries
                    much better than Java seems to. In an entire system, Python can easily
                    be faster because Java includes many slow libraries (slow compared to
                    equivalent libraries available in Python). E.g., Swing is much slower
                    than wxPython.

                    Anyway, that's my take. I'm no authority, as I've never seriously used
                    Java, and haven't done any tests, nor spent anytime looking at the
                    bytecodes, etc.

                    --
                    Ian Bicking / ianb@colorstudy .com / http://blog.ianbicking.org

                    Comment

                    • G. S. Hayes

                      #11
                      Re: why python is slower than java?

                      Maurice LING <mauriceling@ac m.org> wrote in message news:<418b166f$ 1@news.unimelb. edu.au>...[color=blue]
                      > This may be a dumb thing to ask, but besides the penalty for dynamic
                      > typing, is there any other real reasons that Python is slower than Java?[/color]

                      Personally, in practice I haven't found Python to be slower for real
                      apps, although it's sometimes an order of magnitude slower in
                      microbenchmarks . That's probably because a lot of things that could
                      be slow (e.g. numerical manipulation) are backed by C modules behind
                      the scenes in Python; it's also very easy to refactor Python code,
                      which sometimes leads to massive algorithmic improvements.

                      I will say that I still have a tendency to write some projects in C
                      when they seem to warrant it (e.g. the mathematics and video portion
                      of my realtime music visualization system--the UI and database work is
                      in Python).

                      (I also found Java's I/O on our platform to be dog-slow back in the
                      JDK 1.3 days, but I've not used it for anything significant since
                      then.)

                      Comment

                      • Terry Reedy

                        #12
                        Re: why python is slower than java?


                        "Maurice LING" <mauriceling@ac m.org> wrote in message
                        news:418b166f$1 @news.unimelb.e du.au...[color=blue]
                        > This may be a dumb thing to ask, but besides the penalty for dynamic
                        > typing, is there any other real reasons that Python is slower than Java?[/color]

                        To the extent that this is true, development money, though it is sometimes
                        amazing what unpaid or semi-paid volunteers have done with Python.

                        Terry J. Reedy
                        \



                        Comment

                        • John Machin

                          #13
                          Re: why python is slower than java?

                          Maurice LING <mauriceling@ac m.org> wrote in message news:<418b27a1$ 1@news.unimelb. edu.au>...[color=blue]
                          > 1st of all, I thought it is somehow common knowledge that python is
                          > slower than java in many cases. Though I may be wrong... When I do a
                          > Google search, this came up...
                          >
                          > http://twistedmatrix.com/users/glyph...n-vs-java.html[/color]

                          Apart from the execution speed comparison, did you note that the
                          author took 4 x the time to write the Java, even though he had much
                          more Java experience than Python? Did you note that he severely bagged
                          Java reliability?

                          In any case, this article is comparing Python 1.5.2 and Java JDK 1.1
                          -- what does that tell you? If you need a clue, look at the date at
                          the bottom of the article; looks like 4.5 years old to me. Do you
                          think that either or both languages might have changed a little since
                          then?

                          Or is this a joke/troll?? I can't believe the "unimelb.edu.au "; surely
                          a forgery.
                          [color=blue]
                          >
                          > although http://page.mi.fu-berlin.de/~prechel.../jccpprtTR.pdf
                          > showsjust the opposite.
                          >
                          > What I need to work on now is something that requires speed (and dealing
                          > with files), without user's intervention. So the part about users' delay
                          > time is not in the equation. My choices boils down to Python or Java.
                          >
                          > Cheers
                          > maurice[/color]

                          Comment

                          • Hans Nowak

                            #14
                            Re: why python is slower than java?

                            John Machin wrote:
                            [color=blue]
                            > Or is this a joke/troll?? I can't believe the "unimelb.edu.au "; surely
                            > a forgery.[/color]

                            Is it just me, or is the climate in c.l.py getting less friendly to
                            newbies? In any case,
                            http://www.zoology.unimelb.edu.au/staff/nicholas.htm mentions Maurice
                            Ling as an honor student.

                            --
                            Hans Nowak
                            Memimpin Angin Perubahan Teknologi


                            Comment

                            • Lonnie Princehouse

                              #15
                              Re: why python is slower than java?

                              Yes, wxPython is typically quicker than swing. It's not fair to use
                              this in a generic Python-vs-Java speed shootout, though, since
                              wxPython is just a wrapper around wxWidgets' C++ code; you're really
                              comparing C++ vs. Java here. It does highlight the fact that it's
                              easy to write C extensions for Python, though.

                              AFAIK the major two reasons for Python's relative slowness are that
                              it's interpreted and dynamically typed.

                              There's almost always a convenience-for-speed trade-off, and Python is
                              certainly more convenient than Java :P


                              Israel Raj T <rambam@bigpond .net.au> wrote in message news:<87d5ysbkq 9.fsf@pop-server.bigpond. net.au>...[color=blue]
                              > Maurice LING <mauriceling@ac m.org> writes:
                              >[color=green]
                              > > This may be a dumb thing to ask, but besides the penalty for dynamic
                              > > typing, is there any other real reasons that Python is slower than
                              > > Java?[/color]
                              >
                              > Do you have any data to support this ?
                              > On Windows XP, Windows NT and Linux (Gentoo ), I have consistently
                              > found python apps using wxpython to be far faster to load
                              > and to be far more responsive than Java apps.[/color]

                              Comment

                              Working...