speed of python vs matlab.

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

    speed of python vs matlab.

    I've been trying to develop some numerical codes with python, however
    got disappointed.

    A very simple test,

    a = 1.0

    for i in range(1000):
    for j in range(1000):
    a = a+1

    unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
    3.0G, 1G RAM, it varies according to machine configuration, but should
    be in the same level)

    for matlab, the same operation took 0.1 seconds,

    I use numpy & scipy, they solve the problem most of the times, but
    there are cases you can't avoid loops by vectors. I appreciate the
    elegancy of python so much, but I guess I have to gave it up in these
    numerical codes.(image processing algorithms), for application
    dev/scripting, it's still my first choice.

    A good news is that the same code takes ruby 9.8 seconds.

  • Gabriel Genellina

    #2
    Re: speed of python vs matlab.

    At Wednesday 13/12/2006 21:07, Chao wrote:
    >I've been trying to develop some numerical codes with python, however
    >got disappointed.
    >
    >A very simple test,
    >
    >a = 1.0
    >
    >for i in range(1000):
    for j in range(1000):
    a = a+1
    >
    >unfortunatel y, it took 4.5 seconds to finish(my machines is fine. P4
    >3.0G, 1G RAM, it varies according to machine configuration, but should
    >be in the same level)
    How do you measure it? 4.5 secs is far too much. Anyway, try using
    xrange instead of range. This is the standard way to do timings:

    --- cut ---
    def test():
    a = 1.0
    for i in xrange(1000):
    for j in xrange(1000):
    a = a+1

    if __name__=='__ma in__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.repeat(repeat =3,number=1)
    --- cut ---

    I got about 0.24 secs with far less hardware.

    For vector-oriented operations the NumArray package is well suited.


    --
    Gabriel Genellina
    Softlab SRL

    _______________ _______________ _______________ _____
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

    Comment

    • Andrew Sackville-West

      #3
      Re: speed of python vs matlab.

      On Wed, Dec 13, 2006 at 04:07:20PM -0800, Chao wrote:
      I've been trying to develop some numerical codes with python, however
      got disappointed.

      A very simple test,

      a = 1.0

      for i in range(1000):
      for j in range(1000):
      a = a+1

      unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
      3.0G, 1G RAM, it varies according to machine configuration, but should
      be in the same level)
      somethings not right there.

      andrew@debian:~ $ cat pytimetest.py
      a=1.0
      for i in range (1000):
      for j in range (1000):
      a=a+1


      andrew@debian:~ $ time python pytimetest.py

      real 0m0.534s
      user 0m0.528s
      sys 0m0.000s


      andrew@debian:~ $ cat /proc/cpuinfo | grep name
      model name : Intel(R) Celeron(R) CPU 2.53GHz

      andrew@debian:~ $ uname -a
      Linux debian 2.6.18-3-686 #1 SMP Mon Dec 4 16:41:14 UTC 2006 i686
      GNU/Linux

      A

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.6 (GNU/Linux)

      iD8DBQFFgKK8aIe IEqwil4YRAt8uAK CoEAZAZpIpv8aQ9 JVNDls5yVBH5gCe L4WD
      /48x4VpPTrW06O3j jQxSnV0=
      =81y7
      -----END PGP SIGNATURE-----

      Comment

      • Chao

        #4
        Re: speed of python vs matlab.

        My Bad, the time used by python is 0.46~0.49 sec,
        I tried xrange, but it doesn't make things better.

        import time
        tic = time.time()
        a = 1.0

        array = range(1000)

        for i in array:
        for j in array:
        a = a + 0.1

        toc = time.time()
        print toc-tic,' has elapsed'

        used by matlab is 0.012sec

        tic
        a = 1;
        for i=1:1000
        for j=1:1000
        a = a + 1;
        end
        end
        toc

        used by ruby is 0.94~0.96sec

        a = 1
        start = Time.now()

        1000.times do
        1000.times do
        a = a + 1
        end
        end

        finish = Time.now()

        puts finish - start


        Andrew Sackville-West wrote:
        On Wed, Dec 13, 2006 at 04:07:20PM -0800, Chao wrote:
        I've been trying to develop some numerical codes with python, however
        got disappointed.

        A very simple test,

        a = 1.0

        for i in range(1000):
        for j in range(1000):
        a = a+1

        unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
        3.0G, 1G RAM, it varies according to machine configuration, but should
        be in the same level)
        >
        somethings not right there.
        >
        andrew@debian:~ $ cat pytimetest.py
        a=1.0
        for i in range (1000):
        for j in range (1000):
        a=a+1
        >
        >
        andrew@debian:~ $ time python pytimetest.py
        >
        real 0m0.534s
        user 0m0.528s
        sys 0m0.000s
        >
        >
        andrew@debian:~ $ cat /proc/cpuinfo | grep name
        model name : Intel(R) Celeron(R) CPU 2.53GHz
        >
        andrew@debian:~ $ uname -a
        Linux debian 2.6.18-3-686 #1 SMP Mon Dec 4 16:41:14 UTC 2006 i686
        GNU/Linux
        >
        A
        >
        --7AUc2qLy4jB3hD7 Z
        Content-Type: application/pgp-signature
        Content-Disposition: inline;
        filename="signa ture.asc"
        Content-Description: Digital signature
        X-Google-AttachSize: 190

        Comment

        • Jonathan Curran

          #5
          Re: speed of python vs matlab.

          On Wednesday 13 December 2006 18:07, Chao wrote:
          I've been trying to develop some numerical codes with python, however
          got disappointed.
          >
          A very simple test,
          >
          a = 1.0
          >
          for i in range(1000):
          for j in range(1000):
          a = a+1
          >
          unfortunately, it took 4.5 seconds to finish(my machines is fine. P4
          3.0G, 1G RAM, it varies according to machine configuration, but should
          be in the same level)
          >
          for matlab, the same operation took 0.1 seconds,
          >
          I use numpy & scipy, they solve the problem most of the times, but
          there are cases you can't avoid loops by vectors. I appreciate the
          elegancy of python so much, but I guess I have to gave it up in these
          numerical codes.(image processing algorithms), for application
          dev/scripting, it's still my first choice.
          >
          A good news is that the same code takes ruby 9.8 seconds.
          [icicled@A3200 ~]$ time python foo # where foo contained your exact code

          real 0m0.469s
          user 0m0.443s
          sys 0m0.017s

          4.5 seconds? ouch. I've got somewhere near 1 second. Something sounds a little
          fishy b/c my machine is an AMD 3200+ (2.2GHz) w/ 1GB RAM. Yours is a lot
          faster in terms of clock speed.

          Anyway, do take a look at some of the available python compilers. They should
          help considerably.

          - Jonathan

          Comment

          • Christophe

            #6
            Re: speed of python vs matlab.

            Chao a écrit :
            My Bad, the time used by python is 0.46~0.49 sec,
            I tried xrange, but it doesn't make things better.
            >
            import time
            tic = time.time()
            a = 1.0
            >
            array = range(1000)
            >
            for i in array:
            for j in array:
            a = a + 0.1
            >
            toc = time.time()
            print toc-tic,' has elapsed'
            Place all your code inside functions please. IIRC, local variable access
            is much faster that way, and you do a lot of lookup for the a local
            variable in that code.


            import time

            def main():
            a = 1.0

            array = range(1000)

            for i in array:
            for j in array:
            a = a + 0.1

            tic = time.time()
            main()
            toc = time.time()
            print toc-tic,' has elapsed'

            Comment

            • Roberto Bonvallet

              #7
              Re: speed of python vs matlab.

              Chao wrote:
              My Bad, the time used by python is 0.46~0.49 sec,
              I tried xrange, but it doesn't make things better.
              Actually it does: it doesn't waste time and space to create a big list.

              --
              Roberto Bonvallet

              Comment

              • bearophileHUGS@lycos.com

                #8
                Re: speed of python vs matlab.

                Chao, you can also try Psyco, applied on functions, and when necessary
                using its metaclass too.

                Bye,
                bearophile

                Comment

                • Chao

                  #9
                  Re: speed of python vs matlab.

                  Thank you guys for your interest,

                  I tried two things 1) put code into a function 2) use psyco.

                  1) by putting them into a function, there is a significant improvement,
                  around 30%
                  the running time will be around 0.3sec

                  2) by using psyco, it really does a great job, the running time is
                  around 0.045sec.

                  While trying this another question comes up,
                  psyco seems to be able to optimize built-in functions & user's code, if
                  I call a function from an external library, it seems doesn't help.
                  A simple thing is I placed a = numpy.sin(a) in the loop rather than a =
                  a+1, in this case,
                  psyco doesn't have any improvement(or very little). if I put a =
                  math.sin(a) which is from an built-in function, it can achieve a
                  improvement around 3~4. Could the reason be that numpy.sin is
                  actually calling a C library ?

                  Actually Python does show comparable/better performance than other
                  scripting languages. but I'm just surprised that matlab does a great
                  job compared to python/perl, since matlab is also a interpreted
                  language, I'm expecting it has silimar performance with python.

                  I did some search, in previous discussion, people has compared
                  python/numpy vs matlab,
                  but it is actually comparison between numpy(which is implemented in c)
                  vs matlab.

                  Chao.

                  import psyco
                  #psyco.bind(fun ctest)
                  psyco.full()

                  import numpy
                  import time,math

                  def functest(a):
                  array = xrange(1000)

                  for i in array:
                  for j in array:
                  a = a + 1

                  tic = time.time()

                  a = 1.0
                  functest(a)

                  toc = time.time()
                  print toc-tic,' has elapsed'


                  bearophileHUGS@ lycos.com wrote:
                  Chao, you can also try Psyco, applied on functions, and when necessary
                  using its metaclass too.
                  >
                  Bye,
                  bearophile

                  Comment

                  • Robert Kern

                    #10
                    Re: speed of python vs matlab.

                    Chao wrote:
                    While trying this another question comes up,
                    psyco seems to be able to optimize built-in functions & user's code, if
                    I call a function from an external library, it seems doesn't help.
                    A simple thing is I placed a = numpy.sin(a) in the loop rather than a =
                    a+1, in this case,
                    psyco doesn't have any improvement(or very little). if I put a =
                    math.sin(a) which is from an built-in function, it can achieve a
                    improvement around 3~4. Could the reason be that numpy.sin is
                    actually calling a C library ?
                    The reason for the difference is that psyco recognizes math.sin() and replaces
                    it with equivalent machine code to call the standard C library function sin().
                    It does not recognize numpy.sin(), and it is implemented in C, not Python, so it
                    does not do optimization.
                    Actually Python does show comparable/better performance than other
                    scripting languages. but I'm just surprised that matlab does a great
                    job compared to python/perl, since matlab is also a interpreted
                    language, I'm expecting it has silimar performance with python.
                    Matlab uses a JIT compiler along the lines of psyco for simple operations like
                    the one you are doing.

                    --
                    Robert Kern

                    "I have come to believe that the whole world is an enigma, a harmless enigma
                    that is made terrible by our own mad attempt to interpret it as though it had
                    an underlying truth."
                    -- Umberto Eco

                    Comment

                    • greg

                      #11
                      Re: speed of python vs matlab.

                      Chao wrote:
                      I did some search, in previous discussion, people has compared
                      python/numpy vs matlab,
                      but it is actually comparison between numpy(which is implemented in c)
                      vs matlab.
                      Yes, matlab is operating on whole arrays at a time,
                      like numpy. So it's not surprising that they have
                      comparable performance.

                      --
                      Greg

                      Comment

                      Working...