LoginSignup
1
2

More than 5 years have passed since last update.

めざせpythonライブラリマスター (43)cpmoptimize

Posted at

【ライブラリ説明】

 素早く指数計算を行う

【プログラム】

cpmoptimize.py
# -*- coding: utf-8 -*-

from benchit import BenchIt

b = BenchIt()

def fib(n):
    a = 0
    b = 1
    for i in xrange(n):
        a, b = b, a + b
    return a

result = fib(10 ** 6)

b.mark("not use cpmoptimize")

from cpmoptimize import cpmoptimize

@cpmoptimize()
def fib(n):
    a = 0
    b = 1
    for i in xrange(n):
        a, b = b, a + b
    return a

result = fib(10 ** 6)

b.mark("use cpmoptimize")
b.display()

【結果】

output.text
+---------------------+----------+------+-------+----------+----------+---------+
| Marker              | Method   | Line | Loops | Avg Time |  Runtime | Percent |
+---------------------+----------+------+-------+----------+----------+---------+
| not use cpmoptimize | <module> |   16 |     1 | 28.56600 | 28.56600 |   96.95 |
| use cpmoptimize     | <module> |   30 |     1 |  0.89900 |  0.89900 |    3.05 |
+---------------------+----------+------+-------+----------+----------+---------+
Total runtime: 29.47

【参考サイト】

 pypi
 github
 めざせpythonライブラリマスター (27)benchit

1
2
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
1
2