On Mon, Nov 30, 2020 at 10:14 AM Marco Sulla <marco.sulla.pyt...@gmail.com> wrote: > > 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: >
No, Serhiy meant that you can pass a function to timeit. >>> import hashlib >>> def f(): ... h = hashlib.sha256(b"This is a test").hexdigest() ... >>> import timeit >>> timeit.timeit(f) 0.6120481300167739 >>> ChrisA _______________________________________________ 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/6RWV7HWZ7Q5KTMRUXIA6P255RA2BRZUS/ Code of Conduct: http://python.org/psf/codeofconduct/