Recoding closures in C

Discussion in 'Python' started by Jacek Generowicz, Jun 10, 2004.

  1. Following on from my "Optimizing multiple dispatch" question last
    week:

    Thanks to those who offered suggestions: recoding
    tuple(map(type,args)) in C gives me a 20% speed improvement in "The
    Politically Important Benchmark".

    Now, in reaity, my code looks a bit more like this [though, still, I
    am bending the truth siginificantly, in order to hide irrelevant
    domain-specific details]:

    class MethodCollection:

    ...

    def getMethod(dispatcher_self):

    def dispatching_method(self, *args):
    the_method = dispatcher_self.methods[tuple(map(type,args))]
    return the_method(*args)

    return dispatching_method

    [Which is then used in a way which is broadly similar to this:

    Foo_bar = MethodCollection()
    Foo_bar.add_method(this)
    Foo_bar.add_method(that)
    Foo_bar.add_method(the_other)
    ....

    Foo = type('Foo', (object,), {'bar': Foo_bar.getMethod()})

    ]

    Now, "The Politically Imoprtant Benchmark" calls various incarantions
    of dispatching_method in its inner loop (and the methods contained by
    dispatching_method don't do much work), so I suspect that I could get
    meaningful speed improvements by recoding dispatching_method in
    C. However, note that it's a closure over dispatcher_self.

    The most obvious way of doing this, seems to be to implement
    dispatching_method as an extension type, with a tp_call slot which
    implements the body of the original dispatching_method.

    Any advice, comments, warnings ?


    As an added complication, I would like the help for any given
    dispatching_method incarnation to display the signatures of the
    methods it holds. That's pretty easy in pure Python, but can it be
    done if dispatching_method is written in C, (bearing in mind that
    dispatching_methods are being created at run-time) ?
     
    Jacek Generowicz, Jun 10, 2004
    #1
    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. Kasper B. Graversen

    Closures in python

    Kasper B. Graversen, Sep 18, 2003, in forum: Python
    Replies:
    11
    Views:
    702
    David Eppstein
    Sep 21, 2003
  2. Gustavo Niemeyer

    Closures

    Gustavo Niemeyer, Apr 29, 2004, in forum: Python
    Replies:
    1
    Views:
    342
    Michele Simionato
    Apr 30, 2004
  3. Michael Sparks

    Re: Closures

    Michael Sparks, Apr 29, 2004, in forum: Python
    Replies:
    1
    Views:
    349
    Robert Kern
    Apr 30, 2004
  4. Q: Recoding some masking routine

    , May 24, 2007, in forum: C Programming
    Replies:
    9
    Views:
    391
  5. ll

    Recoding with Loop

    ll, May 6, 2008, in forum: ASP General
    Replies:
    1
    Views:
    91
    Bob Barrows [MVP]
    May 6, 2008
Loading...

Share This Page