On Sun, 29 Nov 2020 at 21:45, <sdemen...@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) _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/42DTPO2SRVAQYAX3XLVBXTYKFJL5WVL7/ Code of Conduct: http://python.org/psf/codeofconduct/