3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

めざせpythonライブラリマスター (47)deckor

Posted at

#【ライブラリ説明】
 役に立つデコレータが使える

#【プログラム】

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
'''

#【参考サイト】
 pypi
 github

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?