On Sun, Nov 29, 2020 at 10:36 PM <sdementen@gmail.com> wrote:
My needs are: - simple way to instrument my code to log time for a function or a code block (I am not writing a specific script to time/benchmark a specific code) - have the timing sent to some logger - be the less intrusive possible
With the Timer proposed here above (bad naming as Timer already exists in the timeit module, maybe Clocker ?), I can simply: - decorate a function with it and I am done - wrap an existing code with it as context manager
timeit is indeed pretty awkward. Personally, I use iPython's @timeit for quick an dirty timing of a single function, or a few lines of code. But I'm not sure a context manager or decorator is needed, either. They are not hard to write. IN fact, I've seen both used as examples in tutorials, etc, including some of my own :-) https://uwpce-pythoncert.github.io/ProgrammingInPython/modules/Decorators.ht... and https://uwpce-pythoncert.github.io/ProgrammingInPython/exercises/context-man... (those are very simple, more robust versions would be good) I'd be very surprised if there wasn't something on PyPi as well. -- have you looked? I think one reason this hasn't been made more standard is that for inline timing, it's not clear how the results should be stored / presented -- everyone will want to do that their own way (or do a proper profile). But if you do figure out exactly what you want, and can't find it on PyPi, you can write it, and then put it on PyPi, and/or propose it be added to the std lib. -CHB -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython