On Sun, 29 Nov 2020 at 21:45, <[email protected]> wrote:
> To use timeit (or the current Timer class), one has to write the stmt as a
> string which is not convenient (yet I understand that if you want to time a
> code snippet by running it more than once there may be not alternative than
> using stmt as strings)
You can get the code of a function as a string using `inspect`. Don't
know about generic code, maybe with `ast`? Or you can use the
`globals` parameter of timeit and pass the function name, if I
understood what Serhiy meant:
def timefunc(func, *args, name="f", stmt=None, **kwargs):
try:
globs = kwargs.pop("globals")
except KeyError:
globs = {}
globs[name] = func
if stmt is None:
stmt = f"{name}()"
timeit.timeit(stmt, *args, globals=globs, **kwargs)
(not tested)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/42DTPO2SRVAQYAX3XLVBXTYKFJL5WVL7/
Code of Conduct: http://python.org/psf/codeofconduct/