Skip to content

Instantly share code, notes, and snippets.

@nekoya
Last active August 29, 2015 14:21
Show Gist options
  • Save nekoya/6266777814a55b4fa0cc to your computer and use it in GitHub Desktop.
Save nekoya/6266777814a55b4fa0cc to your computer and use it in GitHub Desktop.
from benchmarker import Benchmarker
from itertools import ifilter
f = lambda x: x % 2
with Benchmarker(width=40, loop=1000) as bm:
for _ in bm('filter'):
list(filter(f, xrange(10000)))
for _ in bm('ifilter'):
list(ifilter(f, xrange(10000)))
for _ in bm('list comp'):
[x for x in xrange(10000) if f(x)]
for _ in bm('generator exp'):
list((x for x in xrange(10000) if f(x)))
"""
## benchmarker: release 3.0.1 (for python)
## python platform: linux2 [GCC 4.6.3]
## python version: 2.7.3
## user sys total real
filter 1.0200 0.0000 1.0200 1.0633
ifilter 1.3800 0.0100 1.3900 1.4401
list comp 1.6700 0.0100 1.6800 1.7635
generator exp 1.2800 0.0100 1.2900 1.3989
## Ranking real
filter 1.0633 (100.0%) *************************
generator exp 1.3989 ( 76.0%) *******************
ifilter 1.4401 ( 73.8%) ******************
list comp 1.7635 ( 60.3%) ***************
## Ratio Matrix real [01] [02] [03] [04]
[01] filter 1.0633 100.0% 131.6% 135.4% 165.9%
[02] generator exp 1.3989 76.0% 100.0% 103.0% 126.1%
[03] ifilter 1.4401 73.8% 97.1% 100.0% 122.5%
[04] list comp 1.7635 60.3% 79.3% 81.7% 100.0%
"""
"""
## benchmarker: release 3.0.1 (for python)
## python platform: linux2 [PyPy 2.5.1 with GCC 4.6.3]
## python version: 2.7.9
## python executable: /home/nn/pypy/bin/python
## user sys total real
filter 0.2100 0.0000 0.2100 0.4034
ifilter 2.3800 0.0200 2.4000 2.4312
list comp 0.2500 0.0000 0.2500 0.2558
generator exp 0.4900 0.0000 0.4900 0.4964
## Ranking real
list comp 0.2558 (100.0%) *************************
filter 0.4034 ( 63.4%) ****************
generator exp 0.4964 ( 51.5%) *************
ifilter 2.4312 ( 10.5%) ***
## Ratio Matrix real [01] [02] [03] [04]
[01] list comp 0.2558 100.0% 157.7% 194.1% 950.6%
[02] filter 0.4034 63.4% 100.0% 123.0% 602.6%
[03] generator exp 0.4964 51.5% 81.3% 100.0% 489.8%
[04] ifilter 2.4312 10.5% 16.6% 20.4% 100.0%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment