benchmarker - a good friend for performance

21
Benchmarker a good friend for performance makoto kuwata http://www.kuwata-lab.com/ PyCon mini JP 2011 Lightning Talk

Upload: kwatch

Post on 06-May-2015

3.411 views

Category:

Documents


1 download

DESCRIPTION

(Pycon mini JP 2011 LightningTalk) About benchmark utility for Python.

TRANSCRIPT

Page 1: Benchmarker - A Good Friend for Performance

Benchmarkera good friend for performance

makoto kuwatahttp://www.kuwata-lab.com/

PyCon mini JP 2011 Lightning Talk

Page 2: Benchmarker - A Good Friend for Performance

Profile お前、誰よ?@makotokuwata

http://www.kwuata-lab.com/

Ruby/PHP/Python programmer

Creator of Erubis (*)

Python4PHPer

パイを広げるのに熱心なだけの人間(*) default template engine on Rails 3

Perl is theCHAMPION!

Page 3: Benchmarker - A Good Friend for Performance

Question

Page 4: Benchmarker - A Good Friend for Performance

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))

Page 5: Benchmarker - A Good Friend for Performance

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

Page 6: Benchmarker - A Good Friend for Performance

Which is faster?

## '+=' operatorx = ""for _ in xrange(N): x += s

## '=' and '+'x = ""for _ in xrange(N): x = x + s

https://gist.github.com/801429

Page 7: Benchmarker - A Good Friend for Performance

Benchmark

'+=' op

'=' & '+'

0 0.1 0.2 0.3 0.4 0.5

0.439sec

0.431sec

second per million

Not Same!

Page 8: Benchmarker - A Good Friend for Performance

Which is faster?

## '+=' operatorx = ""for _ in xrange(N): x += s + "!"

## '=' and '+'x = ""for _ in xrange(N): x = x + s + "!"

https://gist.github.com/801429

Page 9: Benchmarker - A Good Friend for Performance

Benchmark

'+=' op

'=' and '+'

0 0.4 0.8 1.2 1.6 2

0.545sec

second per million

Too Slow! Unmeasurable!

Page 10: Benchmarker - A Good Friend for Performance

Why?

## Fastx += s + "!"

## Slowx = (x + s) + "!"

## Fastx = x + (s + "!")

looong str

short str

Page 11: Benchmarker - A Good Friend for Performance

[OT] Python v.s. Perl

## Python (sloooooow!) s + s + s + s + s + s

## Perl (faaaaaast!) $s . $s . $s . $s . $s . $s

((((( ) ) ) ) )

( )

Page 12: Benchmarker - A Good Friend for Performance

[OT] Python v.s. Perl

## Python (sloooooow!) s + s + s + s + s + s

## Perl (faaaaaast!) $s . $s . $s . $s . $s . $sPerl is

AWESOME!

((((( ) ) ) ) )

( )

Page 13: Benchmarker - A Good Friend for Performance

About Benchmarker

Page 14: Benchmarker - A Good Friend for Performance

Benchmarker

Benchmarking utility for Python

A good friend for performance

http://pypi.python.org/pypi/Benchmarker/

Page 15: Benchmarker - A Good Friend for Performance

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))

Page 16: Benchmarker - A Good Friend for Performance

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%

Page 17: Benchmarker - A Good Friend for Performance

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

Page 18: Benchmarker - A Good Friend for Performance

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

Page 19: Benchmarker - A Good Friend for Performance

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()

Page 20: Benchmarker - A Good Friend for Performance

Benchmarker Python SEARCH

Page 21: Benchmarker - A Good Friend for Performance

thank you