## 2014年3月31日月曜日

### Python - Functions and Generators(Timing tools)

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、10.(Timing tools)を解いてみる。

その他参考書籍

10.(Timing tools)

コード(BBEdit)

sample.py

```#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import math
import sys
import timerseqs

l = list(range(10000))

def sqrtSqrt():
for n in l:
res = math.sqrt(n)
return res

def sqrtOperator():
for n in l:
res = n ** 0.5
return res

def sqrtPow():
for n in l:
res = pow(n, 0.5)
return res

print(sys.version)
for test in (sqrtSqrt, sqrtOperator, sqrtPow):
print('{0:12s}: {1:.5f} => {2}'.format(
test.__name__, *timerseqs.bestoftotal(test)))

def dictComprehensions():
return {x:x for x in l}

def dictForLoop():
res = {}
for x in l:
res[x] = x
return res

for test in (dictComprehensions, dictForLoop):
print('{0:20}: {1:.5f}'.format(
test.__name__, timerseqs.bestoftotal(test)[0]))
```

```\$ ./sample.py
3.3.5 (default, Mar 15 2014, 14:51:54)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)]
sqrtSqrt    : 3.30558 => 99.99499987499375
sqrtOperator: 3.45227 => 99.99499987499375
sqrtPow     : 5.06863 => 99.99499987499375
dictComprehensions  : 1.43393
dictForLoop         : 1.88521
\$
```