Closed jcampbell05 closed 1 year ago
How would benchmark then run multiple iterations of that code in the context manager?
Thinking about this a little more then it seems the only way you could do this is using a generator function unless the entire test function was called multiple times.
for _ in benchmark():
print("My benchmarked code goes here")
I'm not sure if there is a way in python of detection calling the function in a generator context vs a standard lib call (perhaps pytest-benchmark can do this by assuming the user wants to use a generator if a lambda isn't passed in as an argument.
But if this isn't ideal then a generator function could be returned from a context manager like so
with benchmark as b:
for _ in b:
print("My benchmarked code goes here")
Given the initial goal
It can be quite awkward to wrap functions into a lambda and so a context manager could simplify some code.
your realisation that this would require some magic such as a special generator object makes it seem to me that maybe status quo is preferable.
It can be quite awkward to wrap functions into a lambda and so a context manager could simplify some code.
The idea being one could do this:
__enter__
would be used to start the time measurement and__exit__
to finish it