benchmarker - a good friend for performance
DESCRIPTION
(Pycon mini JP 2011 LightningTalk) About benchmark utility for Python.TRANSCRIPT
Benchmarkera good friend for performance
makoto kuwatahttp://www.kuwata-lab.com/
PyCon mini JP 2011 Lightning Talk
Profile お前、誰よ?@makotokuwata
http://www.kwuata-lab.com/
Ruby/PHP/Python programmer
Creator of Erubis (*)
Python4PHPer
パイを広げるのに熱心なだけの人間(*) default template engine on Rails 3
Perl is theCHAMPION!
Question
Which is faster?
https://gist.github.com/800384
## plus operatorx = s + s + s + s + s
## formatx = "%s%s%s%s%s" % (s, s, s, s, s)
## joinx = "".join((s, s, s, s, s))
Benchmark
plus op
format
join
0 0.2 0.4 0.6 0.8 1
0.709sec
0.887sec
0.624sec
second per million
'+' op is the fastestfor short strings
Which is faster?
## '+=' operatorx = ""for _ in xrange(N): x += s
## '=' and '+'x = ""for _ in xrange(N): x = x + s
https://gist.github.com/801429
Benchmark
'+=' op
'=' & '+'
0 0.1 0.2 0.3 0.4 0.5
0.439sec
0.431sec
second per million
Not Same!
Which is faster?
## '+=' operatorx = ""for _ in xrange(N): x += s + "!"
## '=' and '+'x = ""for _ in xrange(N): x = x + s + "!"
https://gist.github.com/801429
Benchmark
'+=' op
'=' and '+'
0 0.4 0.8 1.2 1.6 2
0.545sec
second per million
Too Slow! Unmeasurable!
Why?
## Fastx += s + "!"
## Slowx = (x + s) + "!"
## Fastx = x + (s + "!")
looong str
short str
[OT] Python v.s. Perl
## Python (sloooooow!) s + s + s + s + s + s
## Perl (faaaaaast!) $s . $s . $s . $s . $s . $s
((((( ) ) ) ) )
( )
[OT] Python v.s. Perl
## Python (sloooooow!) s + s + s + s + s + s
## Perl (faaaaaast!) $s . $s . $s . $s . $s . $sPerl is
AWESOME!
((((( ) ) ) ) )
( )
About Benchmarker
Benchmarker
Benchmarking utility for Python
A good friend for performance
http://pypi.python.org/pypi/Benchmarker/
Example Codefrom benchmarker import Benchmarkers = "SOS"with Benchmarker() as bm: with bm.empty(): ## empty loop for _ in xrange(1000*1000): pass with bm('plus op'): for _ in xrange(1000*1000): x = s + s + s + s + s with bm('join'): for _ in xrange(1000*1000): x = "".join((s, s, s, s, s))
Output## benchmarker: release 3.0.0 (for python)## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5659)]## python version: 2.5.5## python executable: /usr/local/python/2.5.5/bin/python
## user sys total real(Empty) 0.0700 0.0000 0.0700 0.0733plus op 0.4800 0.0100 0.4900 0.4805format 0.7600 0.0000 0.7600 0.7652join 0.6600 0.0000 0.6600 0.6610
## Ranking realplus op 0.4805 (100.0%) *************************join 0.6610 ( 72.7%) ******************format 0.7652 ( 62.8%) ****************
## Ratio Matrix real [01] [02] [03][01] plus op 0.4805 100.0% 137.6% 159.2%[02] join 0.6610 72.7% 100.0% 115.8%[03] format 0.7652 62.8% 86.4% 100.0%
Remove same fot-stmt## beforewith Benchmarker() as bm: with bm('plus op'): for _ in xrange(1000*1000): x = s + s + s + s + s
## afterwith Benchmarker(loop=1000*1000) as bm: for _ in bm('join'): for _ in xrange(1000*1000): x = s + s + s + s + s
Repeat all benchmarks## beforewith Benchmarker(loop=1000*1000) as bm: # repeat 5 times for i in xrange(5): with bm('plus op'): ...
## afterfor bm in Benchmarker(loop=1000*1000, cycle=5, extra=1): with bm('plus op'): ...
Remove min & max results
Command-line Options
## Help$ python bench1.py -h
## Number of loop$ python bench1.py -n 1000000
## all benchmarks except test1, test2, ...$ python bench1.py -x 'test\d+'
import benchmarkerbenchmarker.cmdopt.parse()
Benchmarker Python SEARCH
thank you