RE: A question on python performance.

Discussion in 'Python' started by Joe Goldthwaite, Sep 27, 2007.

  1. wrote:

    >Makes perfect sense to me! Think about it:
    >
    >method 1: looks up the method directly from the object (fastest)
    >method 2: looks up __class__, then looks up __dict__, then gets the
    >element from __dict__
    >method 3: looks up caller, looks up __class__, looks up __dict__, gets
    >element from __dict__
    >
    >To get the element directly from the object (method 1), Python has to
    >internally check __class__.__dict__[element], which shows why method 1
    >and method 2 are nearly the same speed. The last version has to look
    >up caller in addition to the process described by method 2.
    >
    >The best way to do what you are doing:
    >
    >getattr(self, param)(self, *args)


    I didn't know about the getattr function. I tried to search for that
    type of function but not knowing how to word the search request,
    I couldn't find it. That's a lot cleaner way of doing what I was
    trying to do. I benchmarked it and it's so close in speed to the
    hard-coded method as to be identical.

    Paul Hankin wrote:

    >You're calling a function (getValue) that just calls a method of trend
    >(caller), that just calls another method of trend (Ptd or Qtd or ...).
    >You can skip all these steps, and just call the method yourself: the
    >code that calls getValue(trend, param, per) replace with
    >trend.<something>(per) if you're calling getValue with a static value
    >for param, or getattr(trend, param)(per) if param is dynamic.


    You're right, it makes sense. Thanks for also pointing out the getattr
    function.

    Eric Jones wrote:

    >What you're describing is a case of mulitple dispatch. See http://

    www.artima.com/weblogs/viewpost.jsp?thread=101605 for a short
    >description and (as short) example by Guido. You can probably fill
    >that out and adapt it to your needs. Alternatively, you could look
    >into the multimethods module in the Gnosis Utilities package: http://
    >pypi.python.org/pypi/Gnosis_Utils/1.2.1-a


    Thanks Eric. I read that article on multi-methods but I can't say I
    really understand it. I guess I still think of decorators as the people
    who got the gym ready for the prom. I've tried getting up to speed on
    decorators but I haven't had much success. Like I mentioned previously,
    with Python, I still feel like a newbie.

    Bruno Desthuilliers wrote:

    >IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
    >and per-instance overriding.


    Thanks for your response also Bruno. I don't understand the code you
    posted well enough yet to even ask a useful question. There are a number
    of things I haven't seen before. Now I really feel like a newbie!
    I'm working on it though so I may have some questions later.

    Thanks again for everyone's input. I really appreciate it.
     
    Joe Goldthwaite, Sep 27, 2007
    #1
    1. Advertising

  2. Joe Goldthwaite a écrit :
    (snip)

    > I guess I still think of decorators as the people
    > who got the gym ready for the prom. I've tried getting up to speed on
    > decorators but I haven't had much success.


    The @decorator syntax is just syntactic sugar for a common use of higher
    order functions. So to understand decorators, you first need understand
    the concepts of higher order functions and closures. Both are documented
    in many places on the web.

    > Bruno Desthuilliers wrote:
    >
    >> IOW, direct access to obj.__class__.__dict__ bypasses both inheritence
    >> and per-instance overriding.

    >
    > Thanks for your response also Bruno. I don't understand the code you
    > posted well enough yet to even ask a useful question. There are a number
    > of things I haven't seen before.


    It's very 101 python code, apart from this line :
    c2.dothis = dothat.__get__(c2, type(c2))

    which is a way to dynamically attach a function as method to an
    instance, directly invoking the descriptor protocol (it's documented on
    python.org)

    FWIW, it's most often done using the 'new' module, and possibly less
    confusing:

    import new
    c2.dothis = new.instancemethod(dothat, c2, type(c2))

    > Now I really feel like a newbie!


    My fault - this example wasn't that necessary, the main point was wrt/
    direct access to instance.__class__.__dict__ bypassing normal attribute
    lookup rules so inherited attributes are not resolved...

    > I'm working on it though so I may have some questions later.


    You're welcome.
     
    Bruno Desthuilliers, Sep 28, 2007
    #2
    1. Advertising

  3. Joe Goldthwaite

    Magnus Lycka Guest

    Joe Goldthwaite wrote:
    > I didn't know about the getattr function. I tried to search for that
    > type of function but not knowing how to word the search request,
    > I couldn't find it.


    You should really read through chapter 2 (Built-in Objects) of the
    library reference. All that stuff is core Python functionality that
    you should be aware of. Reading chapter 3 (Built-in Types) is also
    a really good idea. A lot of the rest is good too, but there is
    really no excuse for not knowing the contents of those chapters. ;)
    It's just a few pages, and very useful to know. Read them now!
     
    Magnus Lycka, Sep 28, 2007
    #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. jm
    Replies:
    1
    Views:
    537
    alien2_51
    Dec 12, 2003
  2. Robert Brewer
    Replies:
    1
    Views:
    361
    Dave Benjamin
    Jan 10, 2004
  3. Lucas Hofman
    Replies:
    13
    Views:
    745
  4. cjl
    Replies:
    3
    Views:
    1,009
    John Nagle
    May 21, 2007
  5. Software Engineer
    Replies:
    0
    Views:
    366
    Software Engineer
    Jun 10, 2011
Loading...

Share This Page