From 3200f9a3ab3ba5fc9de3ab070f931aa26e8a719f Mon Sep 17 00:00:00 2001 From: kirbylife Date: Thu, 31 Aug 2023 21:05:46 +0000 Subject: [PATCH] New snippet --- ...on- decorator to-measure-execution-time.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Python- decorator to-measure-execution-time.md diff --git a/Python- decorator to-measure-execution-time.md b/Python- decorator to-measure-execution-time.md new file mode 100644 index 0000000..87f8f98 --- /dev/null +++ b/Python- decorator to-measure-execution-time.md @@ -0,0 +1,33 @@ +# decorator to measure the execution time per line + +This decorator measures the amount of time it takes to execute a function and displays a detailed summary. + +```Python +from cProfile import Profile + +def explain_time(f): + def wrapper(*args, **kwargs): + profile = Profile() + profile.enable() + output = f(*args, **kwargs) + profile.disable() + profile.print_stats() + return output + return wrapper + +@explain_time +def test(): + d = {"a": -1} + return d.get("a") + +>>> test() + 3 function calls in 0.000 seconds + + Ordered by: standard name + + ncalls tottime percall cumtime percall filename:lineno(function) + 1 0.000 0.000 0.000 0.000 :11(test) + 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} + 1 0.000 0.000 0.000 0.000 {method 'get' of 'dict' objects} + +``` \ No newline at end of file