-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathU4-Ejercicio-1.py
More file actions
43 lines (35 loc) · 938 Bytes
/
U4-Ejercicio-1.py
File metadata and controls
43 lines (35 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Crea un decorador que mida el tiempo de ejecución de una función.
Debe mostrar cuántos segundos tardó en ejecutarse.
"""
import time
from functools import wraps
def medir_tiempo(func):
@wraps(func)
def wrapper(*args, **kwargs):
inicio = time.time()
resultado = func(*args, **kwargs)
fin = time.time()
tiempo_ejecucion = fin - inicio
print(f"⏱️ La función '{func.__name__}' tardó {tiempo_ejecucion:.4f} segundos")
return resultado
return wrapper
# Ejemplos de uso
@medir_tiempo
def funcion_rapida():
return sum(range(1000000))
@medir_tiempo
def funcion_lenta():
time.sleep(2)
return "Listo!"
@medir_tiempo
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Probar los decoradores
print("🔧 PROBANDO DECORADOR DE TIEMPO\n")
funcion_rapida()
funcion_lenta()
print(f"Factorial de 10: {factorial(10)}")