Is a "real" C-Python possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raymond Hettinger

    #16
    Re: Is a "real&quot ; C-Python possible?

    On Dec 9, 1:14 pm, "Jack" <nos...@invalid .comwrote:
    I wonder if it's possible to have a Python that's completely (or at
    least for the most part) implemented in C, just like PHP - I think
    this is where PHP gets its performance advantage. Or maybe I'm wrong
    because the core modules that matter are already in C and those Python
    files are really a think wrapper. Anyhow, if would be ideal if Python
    has performance similar to Java, with both being interpreted languages.
    -1 This would seriously muck-up the evolution of the language.
    Having a few building blocks written in C provides a basis
    for writing very fast pure python (for example, sets, heapq,
    itertools).
    Beyond those building blocks, it is a step backwards to write in C.

    Also, if you really need performance, the traditional solutions are to
    use tools like Psyco or Pyrex.


    Raymond

    Comment

    • hdante

      #17
      Re: Is a &quot;real&quot ; C-Python possible?

      On Dec 9, 10:07 pm, "Jack" <nos...@invalid .comwrote:
      I think most Java-Python benchmarks you can find online will indicate
      that Java is a 3-10 times faster. A few here:
      >http://mail.python.org/pipermail/pyt...ry/125789.html
      >http://blog.snaplogic.org/?p=55
      >
      There are lies, damn lies and benchmarks. :)
      >
      Pure Python code is not going to beat Java code until the Python core
      gets a JIT compiler. If you want fair results you have to either
      disable the JIT in Java or use Psyco for Python. Otherwise you are
      comparing the quality of one language implementation to the quality of a
      JIT compiler.
      >
      The second articple does have a column for Psyco. It helps in some areas
      but still not good enough to stand up against Java. Plus, Psyco is not the
      main stream and has stopped development.
      >
      I'm also wondering, if Psyco is the right way to do, any reason it's not
      being integrated into standard Python?
      Instead of recurring to benchmarks, I recommend that you read the
      following:

      Update 3: 7 Years Of YouTube Scalability Lessons In 30 Minutes and YouTube Strategy: Adding Jitte...


      There are no comparisons there, just a sample of what python and
      psyco can achieve. For a language that isn't designed with speed in
      mind, I think that's quite impressive.

      Comment

      • Bruno Desthuilliers

        #18
        Re: Is a &quot;real&quot ; C-Python possible?

        Jack a écrit :
        >>>I'm not sure
        >>>how much of the C-Python is implemented in C but I think the more
        >>>modules implemented in C, the better performance and lower memory
        >>>footprint it will get.
        >>
        >>Prove it. ;-)
        >
        >
        I guess this is subjective :)
        If yes, benchmarks are not an argument. Else, you'll have hard time
        making your point !-)

        (hint: doing objective benchmarking is really a difficult art)
        - that's what I felt in my experience
        with web applications developed in Python and PHP. I wasn't able to
        find a direct comparison online.
        Could it be the case that you are comparing Python CGI scripts with
        mod_php ? Anyway, since php is also usable (hem... maybe not the
        appropriate qualificative) outside Apache, it should quite easy to make
        a more serious test ?

        Seriously: I never saw any benchmark where php was faster than Python
        for any kind of stuff - unless of course you're trying to compare Zope
        running as a CGI script with an hello world PHP script runned by mod_php.

        Comment

        • Bruno Desthuilliers

          #19
          Re: Is a &quot;real&quot ; C-Python possible?

          hunter.grubbs@g mail.com a écrit :
          (snip)
          I'd like to provide some evidence that Python is *faster* than Java.
          Then benchmark the time taken for the interpreter (oops, sorry: "VM") to
          start !-)

          Comment

          • sturlamolden

            #20
            Re: Is a &quot;real&quot ; C-Python possible?

            On 9 Des, 22:14, "Jack" <nos...@invalid .comwrote:
            I understand that the standard Python distribution is considered
            the C-Python. Howerver, the current C-Python is really a combination
            of C and Python implementation. There are about 2000 Python files
            included in the Windows version of Python distribution. I'm not sure
            how much of the C-Python is implemented in C but I think the more
            modules implemented in C, the better performance and lower memory
            footprint it will get.
            Donald Knuth, one of the fathers of modern computer science, is famous
            for stating that "premature optimization is the root of all evil in
            computer science." A typical computer program tends to have
            bottlenecks that accounts for more than 90% of the elapsed run time.
            Directing your optimizations anywhere else is futile.

            Writing a program in C will not improve the speed of your hardware. If
            the bottleneck is a harddisk or a network connection, using C will not
            change that. Disk i/o is a typical example of that. It is not the
            language that determines the speed by which Python or C can read from
            a disk. It is the disk itself.

            I had a data vizualization program that was slowed down by the need to
            move hundreds of megabytes of vertex data to video RAM. It would
            obviously not help to make the handful of OpenGL calls from C instead
            of Python. The problem was the amount of data and the speed of the
            hardware (ram or bus). The fact that I used Python instead of C
            actually helped to make the problem easier to solve.

            We have seen several examples that 'dynamic' and 'interpreted'
            languages can be quite efficient: There is an implementation of Common
            Lisp - CMUCL - that can compete with Fortran in efficiency for
            numerical computing. There are also versions of Lisp than can compete
            with the latest versions of JIT-compiled Java, e.g. SBCL and Allegro.
            As it happens, SBCL and CMUCL is mostly implemented in Lisp. The issue
            of speed for a language like Python has a lot to do with the quality
            of the implementation. What really makes CMUCL shine is the compiler
            that emits efficient native code on the fly. If it is possible to make
            a very fast Lisp, it should be possible to make a very fast Python as
            well. I remember people complaining 10 years ago that 'Lisp is so
            slow'. A huge effort has been put into making Lisp efficient enough
            for AI. I hope Python some day will gain a little from that effort as
            well.

            We have a Python library that allows us to perform a wide range of
            numerical tasks at 'native speed': NumPy (http://www.scipy.org). How
            such array libraries can be used to get excellent speedups is
            explained here: http://home.online.no/~pjacklam/matl...mtt/index.html

            We obviously need more effort to make Python more efficient for CPU
            bound tasks. Particularly JIT compilation like Java, compilation like
            Lisp or data specialization like Psyco.

            But writing larger parts of the standard library in C is not a
            solution.


            Comment

            • sturlamolden

              #21
              Re: Is a &quot;real&quot ; C-Python possible?

              On 9 Des, 23:34, Christian Heimes <li...@cheimes. dewrote:
              Nevertheless it is just one algorithm that beats Python in an area that
              is well known to be slow. Python's numbers are several factors slower
              than C code because the overhead of the dynamic language throws lots of
              data out of the cache line. If you need fast and highly optimized int
              and floating point operations you can rewrite the algorithm in C and
              create a Python interface for it.
              Lisp is a dynamically typed language. CMUCL can compete with Fortran
              for numerical work. SBCL can compete with the Java server VM. If the
              current CPython VM throws data out of the cache line, then it must be
              a design flaw in the VM.



              Comment

              • Aahz

                #22
                Re: Is a &quot;real&quot ; C-Python possible?

                In article <a324fa50-fde9-4173-81f3-f7e756bd46d7@l1 6g2000hsf.googl egroups.com>,
                sturlamolden <sturlamolden@y ahoo.nowrote:
                >
                >Donald Knuth, one of the fathers of modern computer science, is famous
                >for stating that "premature optimization is the root of all evil in
                >computer science."
                From my .sig database:

                "Premature optimization is the root of all evil in programming."
                --C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
                Hoare)
                --
                Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                "Typing is cheap. Thinking is expensive." --Roy Smith

                Comment

                • Bruno Desthuilliers

                  #23
                  Re: Is a &quot;real&quot ; C-Python possible?

                  sturlamolden a écrit :
                  On 9 Des, 23:34, Christian Heimes <li...@cheimes. dewrote:
                  >
                  >
                  >>Nevertheles s it is just one algorithm that beats Python in an area that
                  >>is well known to be slow. Python's numbers are several factors slower
                  >>than C code because the overhead of the dynamic language throws lots of
                  >>data out of the cache line. If you need fast and highly optimized int
                  >>and floating point operations you can rewrite the algorithm in C and
                  >>create a Python interface for it.
                  >
                  >
                  Lisp is a dynamically typed language. CMUCL can compete with Fortran
                  for numerical work. SBCL can compete with the Java server VM. If the
                  current CPython VM throws data out of the cache line, then it must be
                  a design flaw in the VM.
                  Or a lack of time and money. Lisp is one of the older programming
                  languages around, and at a time had BigBucks(tm) invested on it to try
                  and make it practically usable.

                  Comment

                  • sturlamolden

                    #24
                    Re: Is a &quot;real&quot ; C-Python possible?

                    On 10 Des, 23:54, Bruno Desthuilliers
                    <bdesth.quelque ch...@free.quel quepart.frwrote :
                    Or a lack of time and money. Lisp is one of the older programming
                    languages around, and at a time had BigBucks(tm) invested on it to try
                    and make it practically usable.
                    Yes. But strangely enough, the two Lisp implementations that really
                    kick ass are both free and not particularly old. CMUCL and SBCL proves
                    that you can make a dynamic language implementation extremely
                    efficient if you try hard enough. There are also implementations of
                    Scheme (e.g. Bigloo) that shows the same.

                    Comment

                    • sturlamolden

                      #25
                      Re: Is a &quot;real&quot ; C-Python possible?

                      On 10 Des, 23:49, a...@pythoncraf t.com (Aahz) wrote:
                      "Premature optimization is the root of all evil in programming."
                      --C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
                      Hoare)
                      Oh, I was Hoare? Thanks. Anyway, it doesn't change the argument that
                      optimizing in wrong places is a waste of effort.


                      Comment

                      • sturlamolden

                        #26
                        Re: Is a &quot;real&quot ; C-Python possible?

                        On 9 Des, 23:34, Christian Heimes <li...@cheimes. dewrote:
                        >
                        The Ruby developers are allowed to be proud. They were able to optimize
                        some aspects of the implementation to get one algorithm about 14 times
                        faster. That's good work. But why was it so slow in the first place?
                        The thing to notice here is that Congiano spent 31.5 seconds computing
                        36 Fibonacci numbers in Python and 11.9 seconds doing the same in
                        Ruby. Those numbers are ridiculous! The only thing they prove is that
                        Congiano should not be programming computers. Anyone getting such
                        results should take a serious look at their algoritm instead of
                        blaming the language. I don't care if it takes 31.5 seconds to compute
                        36 Fibonacci numbers in Python 2.5.1 with the dumbest possible
                        algorithm.










                        Comment

                        • Kay Schluehr

                          #27
                          Re: Is a &quot;real&quot ; C-Python possible?

                          We obviously need more effort to make Python more efficient for CPU
                          bound tasks. Particularly JIT compilation like Java, compilation like
                          Lisp or data specialization like Psyco.
                          Given that the Python core team has been mostly silent about JIT
                          compilation and Armin Rigos work in particular which started 5 years
                          ago ( Psyco will not be promoted towards Python 3.0 and there is no
                          indication that anyone but Armin would maintain Psyco ) I wonder about
                          this sudden insight. But maybe you can tell us more when you work on
                          such stuff for CPython yourself? Given your status in the core Python
                          team this would have a chance of not being just a loosely coupled
                          independent project as almost all the interesting stuff that happens
                          around Python these days.

                          Comment

                          • Duncan Booth

                            #28
                            Re: Is a &quot;real&quot ; C-Python possible?

                            sturlamolden <sturlamolden@y ahoo.nowrote:
                            On 9 Des, 23:34, Christian Heimes <li...@cheimes. dewrote:
                            >
                            >>
                            >The Ruby developers are allowed to be proud. They were able to
                            >optimize some aspects of the implementation to get one algorithm
                            >about 14 times faster. That's good work. But why was it so slow in
                            >the first place?
                            >
                            The thing to notice here is that Congiano spent 31.5 seconds computing
                            36 Fibonacci numbers in Python and 11.9 seconds doing the same in
                            Ruby. Those numbers are ridiculous! The only thing they prove is that
                            Congiano should not be programming computers. Anyone getting such
                            results should take a serious look at their algoritm instead of
                            blaming the language. I don't care if it takes 31.5 seconds to compute
                            36 Fibonacci numbers in Python 2.5.1 with the dumbest possible
                            algorithm.
                            >
                            Quite so.

                            Take something like

                            and then modify the Python code from the "Ruby smokes Python" article by
                            the addition of @memoize(3) to decorate the otherwise unchanged fib
                            function: the Python runtime drops down to 0.002 seconds.

                            That is just slightly better than Ruby's 11.9 seconds although I'm sure the
                            Ruby code would also gain as much from a memoize decorator.


                            from memoize import memoize

                            @memoize(3)
                            def fib(n):
                            if n == 0 or n == 1:
                            return n
                            else:
                            return fib(n-1) + fib(n-2)

                            from time import clock
                            start = clock()
                            for i in range(36):
                            print "n=%d =%d" % (i, fib(i))
                            print clock()-start


                            When I run this (with output directed to a file: I'm not trying to time
                            windows console speed), the output is:

                            n=0 =0
                            n=1 =1
                            n=2 =1
                            n=3 =2
                            n=4 =3
                            n=5 =5
                            n=6 =8
                            n=7 =13
                            n=8 =21
                            n=9 =34
                            n=10 =55
                            n=11 =89
                            n=12 =144
                            n=13 =233
                            n=14 =377
                            n=15 =610
                            n=16 =987
                            n=17 =1597
                            n=18 =2584
                            n=19 =4181
                            n=20 =6765
                            n=21 =10946
                            n=22 =17711
                            n=23 =28657
                            n=24 =46368
                            n=25 =75025
                            n=26 =121393
                            n=27 =196418
                            n=28 =317811
                            n=29 =514229
                            n=30 =832040
                            n=31 =1346269
                            n=32 =2178309
                            n=33 =3524578
                            n=34 =5702887
                            n=35 =9227465
                            0.0022642542557 8

                            Comment

                            • MonkeeSage

                              #29
                              Re: Is a &quot;real&quot ; C-Python possible?

                              On Dec 11, 3:10 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
                              sturlamolden <sturlamol...@y ahoo.nowrote:
                              On 9 Des, 23:34, Christian Heimes <li...@cheimes. dewrote:
                              >>
                              The Ruby developers are allowed to be proud. They were able to
                              optimize some aspects of the implementation to get one algorithm
                              about 14 times faster. That's good work. But why was it so slow in
                              the first place?
                              >
                              The thing to notice here is that Congiano spent 31.5 seconds computing
                              36 Fibonacci numbers in Python and 11.9 seconds doing the same in
                              Ruby. Those numbers are ridiculous! The only thing they prove is that
                              Congiano should not be programming computers. Anyone getting such
                              results should take a serious look at their algoritm instead of
                              blaming the language. I don't care if it takes 31.5 seconds to compute
                              36 Fibonacci numbers in Python 2.5.1 with the dumbest possible
                              algorithm.
                              >
                              Quite so.
                              >
                              Take something likehttp://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/498110
                              and then modify the Python code from the "Ruby smokes Python" article by
                              the addition of @memoize(3) to decorate the otherwise unchanged fib
                              function: the Python runtime drops down to 0.002 seconds.
                              >
                              That is just slightly better than Ruby's 11.9 seconds although I'm sure the
                              Ruby code would also gain as much from a memoize decorator.
                              >
                              from memoize import memoize
                              >
                              @memoize(3)
                              def fib(n):
                              if n == 0 or n == 1:
                              return n
                              else:
                              return fib(n-1) + fib(n-2)
                              >
                              from time import clock
                              start = clock()
                              for i in range(36):
                              print "n=%d =%d" % (i, fib(i))
                              print clock()-start
                              >
                              When I run this (with output directed to a file: I'm not trying to time
                              windows console speed), the output is:
                              >
                              n=0 =0
                              n=1 =1
                              n=2 =1
                              n=3 =2
                              n=4 =3
                              n=5 =5
                              n=6 =8
                              n=7 =13
                              n=8 =21
                              n=9 =34
                              n=10 =55
                              n=11 =89
                              n=12 =144
                              n=13 =233
                              n=14 =377
                              n=15 =610
                              n=16 =987
                              n=17 =1597
                              n=18 =2584
                              n=19 =4181
                              n=20 =6765
                              n=21 =10946
                              n=22 =17711
                              n=23 =28657
                              n=24 =46368
                              n=25 =75025
                              n=26 =121393
                              n=27 =196418
                              n=28 =317811
                              n=29 =514229
                              n=30 =832040
                              n=31 =1346269
                              n=32 =2178309
                              n=33 =3524578
                              n=34 =5702887
                              n=35 =9227465
                              0.0022642542557 8
                              Another point is, the reason the ruby code shows such a performance
                              increase is because of the way it wraps native (C) types for integers
                              in the the new byte compiler; i.e., it's a directed optimization,
                              which the example code exploits to its full extent. But with
                              dictionary access, for example, python still creams ruby (by a 2/1
                              factor in my tests). Speaking as someone who uses both python and
                              ruby, I can say that ruby 1.9 is approaching python's speed, which is
                              very cool, but is still not quite as fast as python in general (the
                              whole "smokes python" bit is just propaganda that utilizes a specific
                              feature vector, and is generally unhelpful).

                              Regards,
                              Jordan

                              Comment

                              • John Nagle

                                #30
                                Re: Is a &quot;real&quot ; C-Python possible?

                                sturlamolden wrote:
                                On 10 Des, 23:49, a...@pythoncraf t.com (Aahz) wrote:
                                >
                                >"Premature optimization is the root of all evil in programming."
                                >--C.A.R. Hoare (often misattributed to Knuth, who was himself quoting
                                >Hoare)
                                We're ten years into Python, and it's still a naive interpreter.
                                It's time for a serious optimizing compiler. Shed Skin is going
                                in the right direction. But for some reason, people seem to dislike the
                                Shed Skin effort. Its author writes "Am I the only one seeing the potential
                                of an implicitly statically typed Python-like-language that runs at
                                practically the same speed as C++?"

                                "For a set of 27 non-trivial test programs (at about 7,000 lines in total;
                                .... measurements show a typical speedup of 2-40 times over Psyco, about 10 on
                                average, and 2-220 times over CPython, about 35 on average." So that's
                                what's possible.

                                I'm surprised that Google management isn't pushing Guido towards
                                doing something about the performance problem.

                                John Nagle

                                Comment

                                Working...