2 parents 0d2f291 + 74061a5 commit 8e52f8eCopy full SHA for 8e52f8e
1 file changed
timeit_decorator.py
@@ -0,0 +1,27 @@
1
+# filename: timeit_decorator.py
2
+# Run: python timeit_decorator.py
3
+
4
+import time
5
+from functools import wraps
6
7
+def timeit(func):
8
+ @wraps(func)
9
+ def wrapper(*args, **kwargs):
10
+ start = time.perf_counter()
11
+ result = func(*args, **kwargs)
12
+ end = time.perf_counter()
13
+ print(f"{func.__name__} took {(end-start):.6f}s")
14
+ return result
15
+ return wrapper
16
17
+@timeit
18
+def fib(n):
19
+ if n < 2:
20
+ return n
21
+ return fib(n-1) + fib(n-2)
22
23
+if __name__ == "__main__":
24
+ # Small n to avoid too long recursion
25
+ print("fib(10) =", fib(10))
26
27
+# Note: fib is recursive and intentionally slow — useful to show timing.
0 commit comments