Created
December 20, 2011 09:46
-
-
Save yanolab/1500988 to your computer and use it in GitHub Desktop.
benchmark functional programming pypy1.6 - 1.7 and python2.7.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python2.7.1 | |
================== | |
In [1]: import sys | |
In [2]: sys.version | |
Out[2]: '2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) \n[GCC 4.5.2]' | |
In [3]: import p002, p002_functional | |
In [4]: timeit p002.sumevenfibo(4000000) | |
100000 loops, best of 3: 4.31 us per loop | |
In [5]: timeit p002_functional.sumevenfibo(4000000) | |
100000 loops, best of 3: 12.2 us per loop | |
PyPy1.6 | |
================== | |
In [1]: import sys | |
In [2]: sys.version | |
Out[2]: '2.7.1 (d8ac7d23d3ec, Aug 17 2011, 11:51:18)\n[PyPy 1.6.0 with GCC 4.4.3]' | |
In [3]: import p002, p002_functional | |
In [4]: timeit p002.sumevenfibo(4000000) | |
1000000 loops, best of 3: 383 ns per loop | |
In [5]: timeit p002_functional.sumevenfibo(4000000) | |
10000 loops, best of 3: 19.1 us per loop | |
PyPy1.7 | |
================== | |
In [1]: import sys | |
In [2]: sys.version | |
Out[2]: '2.7.1 (7773f8fc4223, Nov 18 2011, 18:47:10)\n[PyPy 1.7.0 with GCC 4.4.3]' | |
In [3]: import p002, p002_functional | |
In [4]: timeit p002.sumevenfibo(4000000) | |
1000000 loops, best of 3: 388 ns per loop | |
In [5]: timeit p002_functional.sumevenfibo(4000000) | |
100000 loops, best of 3: 11.9 us per loop | |
PyPy1.8 | |
================== | |
In [1]: import sys | |
In [2]: sys.version | |
Out[2]: '2.7.2 (0e28b379d8b3, Feb 09 2012, 19:41:19)\n[PyPy 1.8.0 with GCC 4.4.3]' | |
In [3]: import p002, p002_functional | |
In [4]: timeit p002.sumevenfibo(4000000) | |
1000000 loops, best of 3: 270 ns per loop | |
In [5]: timeit p002_functional.sumevenfibo(4000000) | |
10000 loops, best of 3: 40.6 us per loop | |
PyPy1.9 | |
================== | |
In [1]: import sys | |
In [2]: sys.version | |
Out[2]: '2.7.2 (341e1e3821ff, Jun 07 2012, 15:40:31)\n[PyPy 1.9.0 with GCC 4.4.3]' | |
In [3]: import p002, p002_functional | |
In [4]: timeit p002.sumevenfibo(4000000) | |
1000000 loops, best of 3: 265 ns per loop | |
In [5]: timeit p002_functional.sumevenfibo(4000000) | |
10000 loops, best of 3: 33.4 us per loop | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! -*- coding: utf-8 -*- | |
def sumevenfibo(max): | |
total = 0 | |
term1 = term2 = 1 | |
while term2 < max: | |
if term2 % 2 == 0: | |
total += term2 | |
term1, term2 = term2, term1 + term2 | |
return total | |
if __name__ == '__main__': | |
print sumevenfibo(max=4000000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! -*- coding: utf-8 -*- | |
import operator | |
from itertools import takewhile, ifilter, imap, count | |
from functools import partial | |
from math import pow, sqrt | |
_goldratio = (1+sqrt(5)) / 2 | |
def fibo(n): | |
return int((pow(_goldratio, n) - pow(-_goldratio, -n)) / sqrt(5)) | |
# 偶数フィルタ | |
evenfilter = partial(ifilter, lambda x: x % 2 == 0) | |
def sumevenfibo(max): | |
ifibo = imap(fibo, count(0)) # フィボナッチ数生成ジェネレータ | |
under = partial(takewhile, lambda x: x < max) # max以下に限定するフィルタ | |
return reduce(operator.add, under(evenfilter(ifibo))) | |
if __name__ == '__main__': | |
print sumevenfibo(max=4000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment