Trick: Faster functions (by using generators)

Tried a system with Clang as well now:

 27.3 ± 0.1 ns  send
 29.7 ± 0.0 ns  normal

Python: 3.13.4 (tags/v3.13.4:8a526ec7cbe, Jun 15 2025, 10:56:11) [Clang 18.1.8 (Fedora 18.1.8-2.fc40)]

Anyway, yes, this is something I can easily see vary a lot between different Python versions, compilers, compiler options, and machines. Hence the “can be faster” instead of “is faster”.

Mostly I see it as an interesting curiosity. I’ve known it for a few years but almost never used it. The only cases I remember are this where I also found it neat and scalable to have my predicate function’s state represented by “instruction pointer” (simply writing the different conditions one after the other), and this where I rather did it to point out that there’s precedent for iterators having non-dunder methods (since I suggested adding one for itertools.chain and it struck even me as unusual until I remembered that generator iterators have some).

2 Likes