client_python icon indicating copy to clipboard operation
client_python copied to clipboard

Timing generator function

Open samipdahalr opened this issue 2 years ago • 1 comments

Is there any way to time how long it takes to execute a generator function in python with Histogram.time? Right now, it only seems to measure the time to get the generator object. But since generators do lazy computation while actually iterating over it, we don't get full time.

Example:

def generate_n(n):
    for i in range(n):
        yield i

samipdahalr avatar Sep 23 '22 21:09 samipdahalr

I think you should be able to do this with a context manager.

def generate_n(n):
   with hist.time():
      for i in range(n):
          yield i

Unless I'm quite mistaken... I didn't actually test it, but I do think that should work.

jaklaassen-affirm avatar Oct 20 '22 00:10 jaklaassen-affirm