Des décorateurs utiles peuvent être utilisés
deckor.py
# -*- coding: utf-8 -*-
import deckor
@deckor.timer
def fact(x):
if x <= 0:
return 1
return x*fact(x-1)
fact(5)
'''
Time: 0.000000s
Time: 0.000000s
Time: 0.000000s
Time: 0.000000s
Time: 0.000000s
Time: 0.000000s
120
'''
@deckor.cacheit
def fact(x):
if x <= 0:
return 1
return x*fact(x-1)
fact(5)
# 120
@deckor.trace
def fib(x):
if x < 2:
return x
return fib(x-1)+fib(x-2)
print fib(5)
'''
|--> fib(5)
| |--> fib(4)
| | |--> fib(3)
| | | |--> fib(2)
| | | | |--> fib(1)
| | | | | |--> return 1
| | | | |--> fib(0)
| | | | | |--> return 0
| | | | |--> return 1
| | | |--> fib(1)
| | | | |--> return 1
| | | |--> return 2
| | |--> fib(2)
| | | |--> fib(1)
| | | | |--> return 1
| | | |--> fib(0)
| | | | |--> return 0
| | | |--> return 1
| | |--> return 3
| |--> fib(3)
| | |--> fib(2)
| | | |--> fib(1)
| | | | |--> return 1
| | | |--> fib(0)
| | | | |--> return 0
| | | |--> return 1
| | |--> fib(1)
| | | |--> return 1
| | |--> return 2
| |--> return 5
5
'''
Recommended Posts