[Python-Dev] Rewrite @contextlib.contextmanager in C (original) (raw)

Giampaolo Rodola' g.rodola at gmail.com
Mon Aug 8 15:33:16 EDT 2016


import timeit import contextlib

@contextlib.contextmanager def ctx1(): yield

class ctx2: def enter(self): pass def exit(self, *args): pass

t1 = timeit.timeit("with ctx1(): pass", setup="from main import ctx1") t2 = timeit.timeit("with ctx2(): pass", setup="from main import ctx2") print("%.3f secs" % t1) print("%.3f secs" % t2) print("slowdown: -%.2fx" % (t1 / t2))

...with Python 3.5:

1.938 secs 0.443 secs slowdown: -4.37x

I wanted to give it a try rewriting this in C but since @contextmanager has a lot of magic I wanted to ask first whether this 1) is technically possible 2) is desirable. Thoughts?

-- Giampaolo - http://grodola.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20160808/d11013cd/attachment.html>



More information about the Python-Dev mailing list