Psyco performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danmcleran@yahoo.com

    #1

    Psyco performance

    I'm not seeing much benefit from psyco (only 5-10% faster). Maybe this
    example is too trivial? Can someone give me some pointers as to what
    kind of code would see a dramatic benefit?

    Here's the code:

    import time
    import psyco

    n = 100000

    t1 = time.clock()
    l = list(range(0,n) )
    l2 = [x**2 for x in l]
    t2 = time.clock()
    no_psyco = t2 - t1

    psyco.log()
    psyco.full()

    t1 = time.clock()
    l = list(range(0,n) )
    l2 = [x**2 for x in l]
    t2 = time.clock()

    with_psyco = t2 - t1

    print 'without psyco = ',no_psyco
    print 'with psyco = ',with_psyco
    print 'delta = ',(((no_psyco - with_psyco)/no_psyco) * 100),'%'

  • Christophe

    #2
    Re: Psyco performance

    danmcleran@yaho o.com wrote:[color=blue]
    > I'm not seeing much benefit from psyco (only 5-10% faster). Maybe this
    > example is too trivial? Can someone give me some pointers as to what
    > kind of code would see a dramatic benefit?
    >
    > Here's the code:
    >
    > import time
    > import psyco
    >
    > n = 100000
    >
    > t1 = time.clock()
    > l = list(range(0,n) )
    > l2 = [x**2 for x in l]
    > t2 = time.clock()
    > no_psyco = t2 - t1
    >
    > psyco.log()
    > psyco.full()
    >
    > t1 = time.clock()
    > l = list(range(0,n) )
    > l2 = [x**2 for x in l]
    > t2 = time.clock()
    >
    > with_psyco = t2 - t1
    >
    > print 'without psyco = ',no_psyco
    > print 'with psyco = ',with_psyco
    > print 'delta = ',(((no_psyco - with_psyco)/no_psyco) * 100),'%'
    >[/color]

    Place all the code in a function. Even without psyco you might get
    somewhat better performances then. And I doubt psyco can optimise code
    that isn't in a function anyway.

    And lastly, most of the code is probably spend computing x**2 which is
    already optimised C code.

    Comment

    • Gregory Piñero

      #3
      Re: Psyco performance

      What's the reasoning behind requiring everything to be in functions?
      Just curious.

      On 6/20/06, Christophe <chris.cavalari a@free.fr> wrote:
      [color=blue]
      >
      > Place all the code in a function. Even without psyco you might get
      > somewhat better performances then. And I doubt psyco can optimise code
      > that isn't in a function anyway.[/color]

      Comment

      • Amaury Forgeot d'Arc

        #4
        Re: Psyco performance

        Hello,

        Gregory Piñero a écrit :[color=blue]
        > What's the reasoning behind requiring everything to be in functions?
        > Just curious.[/color]

        You may want to read this:



        Psyco has to run the code at least once to emit code specialized for the
        actual data. It works by replacing blocks of code by other blocks,
        optimized for the kind of data seen the previous times.

        On the contrary, the code outside functions is run only once. You'll
        never get the chance to run the optimized version again...

        --
        Amaury

        Comment

        • danmcleran@yahoo.com

          #5
          Re: Psyco performance

          > Place all the code in a function. Even without psyco you might get[color=blue]
          > somewhat better performances then. And I doubt psyco can optimise code
          > that isn't in a function anyway.
          >
          > And lastly, most of the code is probably spend computing x**2 which is
          > already optimised C code.[/color]

          I've changed the code to include a class, method call, and function.
          Now the Psyco code is quite a bit slower. Is this a valid way to test
          Psyco's effects? When I run the following code I get this result:

          without psyco = 0.96840101186
          with psyco = 1.82430169197
          with psyco = 0.855900680114 slower


          The code:

          import time
          import psyco

          class Test(object):
          def __init__(self, value):
          self.value = value

          def foo(self):
          return reduce(lambda x,y : x + y, list(range(0,se lf.value)))

          def test(n):
          l = [Test(i) for i in range(1, n)]
          return [x.foo() for x in l]

          n = 1000

          t1 = time.clock()
          l2 = test(n)
          t2 = time.clock()
          no_psyco = t2 - t1

          psyco.full()

          t1 = time.clock()
          l2 = test(n)
          t2 = time.clock()

          with_psyco = t2 - t1

          print 'without psyco = ',no_psyco
          print 'with psyco = ',with_psyco
          delta = (no_psyco - with_psyco)
          if(delta > 0):
          result = 'faster'
          else:
          result = 'slower'

          print 'with psyco = ',abs(delta),re sult

          Comment

          • Paul McGuire

            #6
            Re: Psyco performance

            <danmcleran@yah oo.com> wrote in message
            news:1150839749 .939196.142440@ r2g2000cwb.goog legroups.com...[color=blue][color=green]
            > > Place all the code in a function. Even without psyco you might get
            > > somewhat better performances then. And I doubt psyco can optimise code
            > > that isn't in a function anyway.
            > >
            > > And lastly, most of the code is probably spend computing x**2 which is
            > > already optimised C code.[/color]
            >
            > I've changed the code to include a class, method call, and function.
            > Now the Psyco code is quite a bit slower. Is this a valid way to test
            > Psyco's effects? When I run the following code I get this result:
            >
            > without psyco = 0.96840101186
            > with psyco = 1.82430169197
            > with psyco = 0.855900680114 slower
            >
            >[/color]
            Here are 3 different implementations of foo, with varying degrees of
            improvement.

            func without with
            foo1: 0.1727 0.0106
            foo2: 0.1020 0.1012
            foo3: 0.3632 0.8068

            foo1 is just a brute force for-loop summing the values of the composed list,
            foo2 calls sum(), and foo3 is the original foo using reduce().
            Surprisingly, brute force + psyco beats reduce and sum without psyco.

            psyco's strength is in compiling Python code inside functions. In foo2 and
            foo3, most of the processing is done not in explicit Python, but in C code
            implementation of sum and reduce, so the psyco processing is actually adding
            more than it is optimizing.

            -- Paul


            import time
            import psyco
            time.clock()

            class Test(object):
            def __init__(self, value):
            self.value = value

            def foo1(self):
            z = 0
            for i in range(self.valu e):
            z += i
            return z

            def foo2(self):
            return sum(list(range( 0,self.value)))

            def foo3(self):
            return reduce(lambda x,y : x + y, list(range(0,se lf.value)))

            def test(n,f):
            l = [Test(i) for i in range(1, n)]
            return [f(x) for x in l]

            n = 1000
            fns = (Test.foo1, Test.foo2, Test.foo3)
            no_psyco = []
            with_psyco = []

            for fn in fns:
            t1 = time.clock()
            l2 = test(n,fn)
            t2 = time.clock()
            no_psyco.append ( t2 - t1 )

            psyco.full()

            for fn in fns:
            t1 = time.clock()
            l2 = test(n,fn)
            t2 = time.clock()
            with_psyco.appe nd( t2 - t1 )

            for fnData in zip([f.func_name for f in fns],no_psyco,with_ psyco):
            print "%s: %.4f %.4f" % fnData


            Comment

            • danmcleran@yahoo.com

              #7
              Re: Psyco performance

              > > > Place all the code in a function. Even without psyco you might get[color=blue][color=green][color=darkred]
              > > > somewhat better performances then. And I doubt psyco can optimise code
              > > > that isn't in a function anyway.[/color][/color][/color]

              Another thing I wasn't considering is that the first call with psyco
              enabled might be slower. The 2nd time the psyco-compiled function is
              called is where the speed improvement may be present. With the code at
              the bottom, I get these results:

              without psyco = 0.0004212825931 79
              first call with psyco = 0.0009023493209 33
              with psyco = 5.30793718196e-005
              first call with psyco = 114.190981432 % slower
              2nd call with psyco = 87.400530504 % faster


              import time
              import psyco

              def test(l):
              result = 0

              for item in l:
              result += item

              return result

              l = list(range(0, 1000))

              t1 = time.clock()
              l2 = test(l)
              t2 = time.clock()
              no_psyco = t2 - t1

              psyco.log()
              psyco.bind(test )

              t1 = time.clock()
              l2 = test(l)
              t2 = time.clock()

              first_call_with _psyco = t2 - t1

              t1 = time.clock()
              l2 = test(l)
              t2 = time.clock()

              with_psyco = t2 - t1

              print 'without psyco = ',no_psyco
              print 'first call with psyco = ',first_call_wi th_psyco
              print 'with psyco = ',with_psyco
              first_delta = ((no_psyco - first_call_with _psyco)/no_psyco) * 100
              delta = ((no_psyco - with_psyco)/no_psyco) * 100

              if(first_delta > 0):
              result = 'faster'
              else:
              result = 'slower'

              print 'first call with psyco = ',abs(first_del ta),'% ',result

              if(delta > 0):
              result = 'faster'
              else:
              result = 'slower'

              print '2nd call with psyco = ',abs(delta),'% ',result

              Comment

              Working...