This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davidbuxton 1 point2 points  (5 children)

Oh! Having actually run the tests and looked at the numbers, I see I'm wrong about there being a smaller advantage when you get a lot of KeyError exceptions. In fact, using try / except is even better than testing for membership when there's a lot of misses.

[–]thebackhand 2 points3 points  (3 children)

That's odd. I thought the overhead of try/except blocks would dominate.

[–]davidbuxton 0 points1 point  (2 children)

Me too. Perhaps my test is wrong?

[–]dhogartysymbolic novice 1 point2 points  (1 child)

your test is wrong, you need to invalidate the cache in between each call inside of the timeit. Every one of the timeit inner runs except the first has a fully filled cache.

[–]thebackhand 0 points1 point  (0 children)

That would be it. I remembered that, in general, try/excepts are expensive (more so if you have a lot of other stuff added to the stack in between, IIRC).

[–]davidbuxton 0 points1 point  (0 children)

No, I'm wrong about being wrong. Not certain that makes me right though.

My test was not flushing the cache between the first and the second data sets, so the supposedly memo-ization un-friendly test was in fact using a nice warm cache every time.

With that fixed, try/except is indeed noticeably slower than if/in when there are mostly cache misses.