On Sun, 29 Nov 2020 at 21:45, <sdementen@gmail.com> 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)