Skip to content

Instantly share code, notes, and snippets.

@tingletech
Last active September 13, 2016 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tingletech/c2e3c0ce666cbd236be11df349c5973c to your computer and use it in GitHub Desktop.
Save tingletech/c2e3c0ce666cbd236be11df349c5973c to your computer and use it in GitHub Desktop.
pip install pytest-benchmark statistics
from __future__ import print_function
from xml.etree.ElementTree import Element, SubElement, ElementTree
from lxml.etree import Element as E
from lxml.etree import SubElement as SE
from lxml.etree import ElementTree as ET
def tree(f):
# https://pymotw.com/2/xml/etree/ElementTree/create.html#serializing-xml-to-a-stream
url = Element('url')
loc = SubElement(url, 'loc')
loc.text = u'https://calisphere.org/item/{0}/'.format('xxxxxxx')
ElementTree(url).write(f)
def lxmltree(f):
url = E('url')
loc = SE (url, 'loc')
loc.text = u'https://calisphere.org/item/{0}/'.format('xxxxxxx')
ET(url).write(f)
def print_template(f):
print(
u'<url><loc>https://calisphere.org/item/{0}/</loc></url>'.format('xxxxxxx'),
file=f, end=""
)
def fwrite(f):
f.write(u'<url><loc>https://calisphere.org/item/{0}/</loc></url>'.format('xxxxxxx'))
def test_etree(benchmark):
with open('/dev/null', 'w+') as f:
result = benchmark(tree, f)
def test_lxmletree(benchmark):
with open('/dev/null', 'w+') as f:
result = benchmark(lxmltree, f)
def test_print(benchmark):
with open('/dev/null', 'w+') as f:
result = benchmark(print_template, f)
def test_fwrite(benchmark):
with open('/dev/null', 'w+') as f:
result = benchmark(fwrite, f)
py.test benchmark.py --benchmark-min-rounds 10000
=========================================================================== test session starts ===========================================================================
platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
benchmark: 3.0.0 (defaults: timer=time.time disable_gc=False min_rounds=10000 min_time=5.00us max_time=1.00s calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /Users/btingle/workspace, inifile:
plugins: benchmark-3.0.0
collected 4 items
benchmark.py ....
------------------------------------------------------------------------------------- benchmark: 4 tests -------------------------------------------------------------------------------------
Name (time in ns) Min Max Mean StdDev Median IQR Outliers(*) Rounds Iterations
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_print 0.0000 (1.0) 141,859.0546 (1.31) 3,410.8497 (1.12) 1,305.5858 (1.0) 3,099.4415 (1.0) 1,192.0929 (5.00) 944;653 62602 1
test_fwrite 1,907.3486 (inf) 154,972.0764 (1.43) 3,036.4342 (1.0) 1,414.1164 (1.08) 3,099.4415 (1.0) 238.4186 (1.0) 448;11726 47663 1
test_lxmletree 12,874.6033 (inf) 108,003.6163 (1.0) 15,907.3114 (5.24) 3,420.1570 (2.62) 15,020.3705 (4.85) 2,861.0229 (12.00) 375;315 10000 1
test_etree 27,894.9738 (inf) 581,026.0773 (5.38) 32,045.5313 (10.55) 7,473.7609 (5.72) 30,040.7410 (9.69) 4,768.3716 (20.00) 349;263 10000 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(*) Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
======================================================================== 4 passed in 4.57 seconds =========================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment