2015/01/16(金)Python勉強メモ#4「ベンチマークをとれ」

PyPIにある「Benchmarker」を利用。コードはひらがなリストを作る処理です。リスト内包表記が最速でした。

#!/usr/bin/env python

from benchmarker import Benchmarker

with Benchmarker(1000*100, width=20) as bench:
    code_point = range(0x3040, 0x30A0)

    @bench('1')
    def _(bm):
        for i in bm:
            moji_list = []
            for cp in code_point: moji_list.append( chr(cp) )

    @bench('2')
    def _bm(bm):
        for i in bm:
            moji_list = [ chr(cp) for cp in code_point ]

    @bench('3')
    def _bm(bm):
        for i in bm:
            moji_list = list(chr(cp) for cp in code_point)
## benchmarker:         release 4.0.1 (for python)
## python version:      3.4.2
## python compiler:     GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)
## python platform:     Darwin-14.0.0-x86_64-i386-64bit
## python executable:   /Users/username/.anyenv/envs/pyenv/versions/3.4.2/bin/python
## cpu model:           Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz 
## parameters:          loop=100000, cycle=1, extra=0

##                        real    (total    = user    + sys)
1                       3.1367    3.0500    3.0200    0.0300
2                       2.4863    2.4300    2.4200    0.0100
3                       2.8027    2.7300    2.7100    0.0200

## Ranking                real
2                       2.4863  (100.0) ********************
3                       2.8027  ( 88.7) ******************
1                       3.1367  ( 79.3) ****************

## Matrix                 real    [01]    [02]    [03]
[01] 2                  2.4863   100.0   112.7   126.2
[02] 3                  2.8027    88.7   100.0   111.9
[03] 1                  3.1367    79.3    89.3   100.0