On 8/8/2016 22:38, Yury Selivanov wrote: > > > On 2016-08-08 4:18 PM, Guido van Rossum wrote: >> I think Nick would be interested in understanding why this is the >> case. What does the decorator do that could be so expensive? > > From the looks of it it doesn't do anything special. Although with > @contextlib.contextmanager we have to instantiate a generator (the > decorated one) and advance it in __enter__. So it's an extra object > instantiation + extra code in __enter__ and __exit__. Anyways, Nick > knows much more about that code. > Right, I think a fairer comparison would be to: class ctx2: def __enter__(self): self.it = iter(self) return next(self.it) def __exit__(self, *args): try: next(self.it) except StopIteration: pass def __iter__(self): yield With this change alone the slowdown diminishes to ~ 1.7x for me. The rest is probably the extra overhead for being able to pass exceptions raised inside the with block back into the generator and such.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4