Ethan Furman wrote:
Steven D'Aprano wrote:
On Fri, 02 Apr 2010 19:48:59 -0700, Ethan Furman wrote:


The heuristic I use is, if I expect the try block to raise an exception
more than about one time in ten, I change to an explicit test. In this
case, since the exception should only be raised once, and then never
again, I would use a try...except block.

That was my reasoning as well, but when I timed it for one million runs
(so 1 instantiation, 999,999 simple calls), the __getattr__ time was .5
seconds, the try...execpt block was .6; at ten million it was 5 and 6.


Care to share your timing code? Not that I don't trust your results, but timings are very sensitive to the exact thing you do, and I'd like to see what that is.

Happy to do so -- if I made a mistake I'd like to know about it and learn.

It'll have to wait two days 'til I get back to work, though... I'll post it asap.

Well, so much for asap, but here's what I used (with one correction: in the 'if' code I had forgotten to actually reference the missing attribute, so the __getattr__ look up never happened; now the try...except block is /slightly/ faster, as opposed to 20% slower).


class spam_except(object):
    def __call__(self, x, y, z):
        try:
            mongo = self.mongo
        except AttributeError:
            mongo = self.mongo = 1
        return

class spam_if(object):
    def __getattr__(self, name):
        if name != 'mongo':
            raise AttributeError
        self.mongo = 1
        return self.mongo
    def __call__(self, x, y, z):
        self.mongo      # didn't have this line before.  d'oh!
        return


--> timeit.Timer('spammer(1,2,3)','from spam import spam_except; spammer=spam_except()').timeit()
0.65764130543749388

--> timeit.Timer('spammer(1,2,3)','from spam import spam_if; spammer=spam_if()').timeit()
0.66972877235545525

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to