Re: Psycho question

Discussion in 'Python' started by bearophileHUGS@lycos.com, Aug 6, 2008.

  1. Guest

    David C. Ullrich:
    > Thanks. If I can get it installed and it works as advertised
    > this means I can finally (eventually) finish the process of
    > dumping MS Windows: the only reason I need it right now is for
    > the small number of Delphi programs I have for which straight
    > Python is really not adequate. Been not looking forward to
    > learning some C or Objective C (or whatever that Mac thing
    > is) - if I can just "accelerate" a few Python routines that'll
    > be great.


    To have better performance with Psyco you need low-level style code,
    generally not lazy, etc, and adopt some programming conventions, so
    you may have to rewrite your routines for max speed.

    If some of your routines are too much slow there are many ways in
    Python to write faster modules, like Cython, Weave, Inline, Swig, SIP,
    ShedSkin, etc. For bioinformatics purposes I have found that Pyd + D
    language is good for me (I have tried Pyrex too few times, but I have
    lost my patience trying to track down in a jungle of ugly auto-
    generated C code where some reference count updates happen. Writing D
    code is hugely faster/better for me. Even writing a C extension for
    Python from scratch may be better for me because there aren't hidden
    things happening everywhere. I presume other people don't share this
    problems of mine because there are lot of people using Cython now).

    Bye,
    bearophile
     
    , Aug 6, 2008
    #1
    1. Advertising

  2. Guest

    Erik Max Francis:
    > If len(bytes) is large, you might want to use `xrange`, too. `range`
    > creates a list which is not really what you need.


    That's right for Python, but Psyco uses normal loops in both cases,
    you can time this code in the two situations:

    def foo1(n):
    count = 0
    for i in range(n):
    count += 1
    print count

    def foo2(n):
    count = 0
    for i in xrange(n):
    count += 1
    print count

    import psyco; psyco.full()
    N = 100000000
    #foo1(N)
    foo2(N)

    Bye,
    bearophile
     
    , Aug 6, 2008
    #2
    1. Advertising

  3. MRAB Guest

    On Aug 6, 8:52 pm, "David C. Ullrich" <> wrote:
    > In article
    > <>,
    >
    >   wrote:
    > > David C. Ullrich:
    > > > Thanks. If I can get it installed and it works as advertised
    > > > this means I can finally (eventually) finish the process of
    > > > dumping MS Windows: the only reason I need it right now is for
    > > > the small number of Delphi programs I have for which straight
    > > > Python is really not adequate. Been not looking forward to
    > > > learning some C or Objective C (or whatever that Mac thing
    > > > is) - if I can just "accelerate" a few Python routines that'll
    > > > be great.

    >
    > > To have better performance with Psyco you need low-level style code,
    > > generally not lazy, etc, and adopt some programming conventions, so
    > > you may have to rewrite your routines for max speed.

    >
    > Thanks. I would have guessed that I'd want low-level style code;
    > that's the sort of thing I have in mind. In fact the only thing
    > that seems likely to come up right now is looping through an
    > array of bytes, modifying them. The plan is to use the array
    > module first to convert a string or a list to an array, outside
    > the accelerated part, then maybe do something like
    >
    > for j in range(len(bytes)/3):
    >   g = (bytes[3*j] + bytes[3*j+1] + bytes[3*j+2])/3
    >   bytes[3*j] = bytes[3*j+1] = bytes[3*j+2] = g
    >
    > then convert back to a list or string or whatever outside
    > the accelerated function.
    >

    [snip]
    A couple of points:

    1. '/' with ints in Python 2.x returns an int, but from Python 3.x
    it'll return a float. You're recommended to use '//' for int division.

    2. 'range' can accept a step value, so you can rewrite that as:

    for j in range(0, len(bytes), 3):
    g = (bytes[j] + bytes[j+1] + bytes[j+2])//3 # I think you also
    want // here
    bytes[j] = bytes[j+1] = bytes[j+2] = g
     
    MRAB, Aug 7, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    597
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. Sharon McCarty
    Replies:
    1
    Views:
    374
    Scott Allen
    Nov 13, 2004
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    663
    Iain Norman
    Feb 16, 2005
  4. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    950
    Scott Allen
    Oct 6, 2005
  5. Delaney, Timothy (Tim)

    RE: Psycho question

    Delaney, Timothy (Tim), Aug 8, 2008, in forum: Python
    Replies:
    4
    Views:
    343
Loading...

Share This Page