Created
January 29, 2011 02:17
-
-
Save kwatch/801429 to your computer and use it in GitHub Desktop.
Benchmarker example: '+=' v.s. '=' & '+'
This file contains hidden or 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 -*- | |
## | |
## requires Benchmarker 3.0 | |
## http://pypi.python.org/pypi/Benchmarker/ | |
## | |
from __future__ import with_statement | |
from benchmarker import Benchmarker, cmdopt | |
cmdopt.parse() | |
flag_all = cmdopt.get('all') | |
#loop = 100000 | |
loop = 1000000 | |
s = "SOS" * 10 | |
with Benchmarker(width=25) as bm: | |
#for bm in Benchmarker(width=25, cycle=5, extra=1): | |
with bm.empty(): ## empty loop | |
pass | |
with bm("x += s"): | |
x = "" | |
for i in xrange(loop): | |
x += s | |
with bm("x = x + s"): | |
x = "" | |
for i in xrange(loop): | |
x = x + s | |
with bm("x += s + '!'"): | |
x = "" | |
for i in xrange(loop): | |
x += s + '!' | |
if flag_all: | |
with bm("x = x + s + '!'"): ## VERY SLOW | |
x = "" | |
for i in xrange(loop): | |
x = x + s + '!' | |
with bm("x = x + (s + '!')"): | |
x = "" | |
for i in xrange(loop): | |
x = x + (s + '!') | |
### | |
### output example | |
### | |
# $ py ex02.py [--all] | |
# ## benchmarker: release 3.0.0 (for python) | |
# ## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5664)] | |
# ## python version: 2.7.1 | |
# ## python executable: /usr/local/python/2.7.1/bin/python | |
# | |
# ## user sys total real | |
# (Empty) 0.0000 0.0000 0.0000 0.0000 | |
# x += s 0.3700 0.0500 0.4200 0.4227 | |
# x = x + s 0.3800 0.0600 0.4400 0.4319 | |
# x += s + '!' 0.4800 0.0600 0.5400 0.5394 | |
# x = x + (s + '!') 0.4800 0.0600 0.5400 0.5456 | |
# | |
# ## Ranking real | |
# x += s 0.4227 (100.0%) ************************* | |
# x = x + s 0.4319 ( 97.9%) ************************ | |
# x += s + '!' 0.5394 ( 78.4%) ******************** | |
# x = x + (s + '!') 0.5456 ( 77.5%) ******************* | |
# | |
# ## Ratio Matrix real [01] [02] [03] [04] | |
# [01] x += s 0.4227 100.0% 102.2% 127.6% 129.1% | |
# [02] x = x + s 0.4319 97.9% 100.0% 124.9% 126.3% | |
# [03] x += s + '!' 0.5394 78.4% 80.1% 100.0% 101.2% | |
# [04] x = x + (s + '!') 0.5456 77.5% 79.2% 98.9% 100.0% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment