On Mon, Nov 30, 2020 at 10:14 AM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
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:
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