Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added useful tools for testing

darcs-hash:20070427152713-a5408-699ad830f834bd18ec62ab3544668fd648b36dc2.gz
  • Loading branch information...
commit a117b22936b63d44c3b1d563432e4861035329f0 1 parent a79215a
@DanielStutzbach authored
Showing with 585 additions and 2 deletions.
  1. +1 −1  README.txt
  2. +228 −0 ez_setup.py
  3. +14 −0 replot.py
  4. +1 −1  setup.py
  5. +341 −0 speed_test.py
View
2  README.txt
@@ -23,7 +23,7 @@ Example:
None of the above operations have a noticeable delay, even though the
lists have over 500 million elements due to line 3. The BList has two
-key features that allow it to pull this off this performance:
+key features that allow it to pull off this performance:
1. Internally, a B+Tree is a wide, squat tree. Each node has a
maximum of 128 children. If the entire list contains 128 or fewer
View
228 ez_setup.py
@@ -0,0 +1,228 @@
+#!python
+"""Bootstrap setuptools installation
+
+If you want to use setuptools in your package's setup.py, just include this
+file in the same directory with it, and add this to the top of your setup.py::
+
+ from ez_setup import use_setuptools
+ use_setuptools()
+
+If you want to require a specific version of setuptools, set a download
+mirror, or use an alternate download directory, you can do so by supplying
+the appropriate options to ``use_setuptools()``.
+
+This file can also be run as a script to install or upgrade setuptools.
+"""
+import sys
+DEFAULT_VERSION = "0.6c5"
+DEFAULT_URL = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3]
+
+md5_data = {
+ 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
+ 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
+ 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
+ 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
+ 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
+ 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
+ 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
+ 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
+ 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
+ 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
+ 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
+ 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
+ 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
+ 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
+ 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
+ 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
+ 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
+ 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
+ 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
+ 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
+ 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
+}
+
+import sys, os
+
+def _validate_md5(egg_name, data):
+ if egg_name in md5_data:
+ from md5 import md5
+ digest = md5(data).hexdigest()
+ if digest != md5_data[egg_name]:
+ print >>sys.stderr, (
+ "md5 validation of %s failed! (Possible download problem?)"
+ % egg_name
+ )
+ sys.exit(2)
+ return data
+
+
+def use_setuptools(
+ version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
+ download_delay=15
+):
+ """Automatically find/download setuptools and make it available on sys.path
+
+ `version` should be a valid setuptools version number that is available
+ as an egg for download under the `download_base` URL (which should end with
+ a '/'). `to_dir` is the directory where setuptools will be downloaded, if
+ it is not already available. If `download_delay` is specified, it should
+ be the number of seconds that will be paused before initiating a download,
+ should one be required. If an older version of setuptools is installed,
+ this routine will print a message to ``sys.stderr`` and raise SystemExit in
+ an attempt to abort the calling script.
+ """
+ try:
+ import setuptools
+ if setuptools.__version__ == '0.0.1':
+ print >>sys.stderr, (
+ "You have an obsolete version of setuptools installed. Please\n"
+ "remove it from your system entirely before rerunning this script."
+ )
+ sys.exit(2)
+ except ImportError:
+ egg = download_setuptools(version, download_base, to_dir, download_delay)
+ sys.path.insert(0, egg)
+ import setuptools; setuptools.bootstrap_install_from = egg
+
+ import pkg_resources
+ try:
+ pkg_resources.require("setuptools>="+version)
+
+ except pkg_resources.VersionConflict, e:
+ # XXX could we install in a subprocess here?
+ print >>sys.stderr, (
+ "The required version of setuptools (>=%s) is not available, and\n"
+ "can't be installed while this script is running. Please install\n"
+ " a more recent version first.\n\n(Currently using %r)"
+ ) % (version, e.args[0])
+ sys.exit(2)
+
+def download_setuptools(
+ version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
+ delay = 15
+):
+ """Download setuptools from a specified location and return its filename
+
+ `version` should be a valid setuptools version number that is available
+ as an egg for download under the `download_base` URL (which should end
+ with a '/'). `to_dir` is the directory where the egg will be downloaded.
+ `delay` is the number of seconds to pause before an actual download attempt.
+ """
+ import urllib2, shutil
+ egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
+ url = download_base + egg_name
+ saveto = os.path.join(to_dir, egg_name)
+ src = dst = None
+ if not os.path.exists(saveto): # Avoid repeated downloads
+ try:
+ from distutils import log
+ if delay:
+ log.warn("""
+---------------------------------------------------------------------------
+This script requires setuptools version %s to run (even to display
+help). I will attempt to download it for you (from
+%s), but
+you may need to enable firewall access for this script first.
+I will start the download in %d seconds.
+
+(Note: if this machine does not have network access, please obtain the file
+
+ %s
+
+and place it in this directory before rerunning this script.)
+---------------------------------------------------------------------------""",
+ version, download_base, delay, url
+ ); from time import sleep; sleep(delay)
+ log.warn("Downloading %s", url)
+ src = urllib2.urlopen(url)
+ # Read/write all in one block, so we don't create a corrupt file
+ # if the download is interrupted.
+ data = _validate_md5(egg_name, src.read())
+ dst = open(saveto,"wb"); dst.write(data)
+ finally:
+ if src: src.close()
+ if dst: dst.close()
+ return os.path.realpath(saveto)
+
+def main(argv, version=DEFAULT_VERSION):
+ """Install or upgrade setuptools and EasyInstall"""
+
+ try:
+ import setuptools
+ except ImportError:
+ egg = None
+ try:
+ egg = download_setuptools(version, delay=0)
+ sys.path.insert(0,egg)
+ from setuptools.command.easy_install import main
+ return main(list(argv)+[egg]) # we're done here
+ finally:
+ if egg and os.path.exists(egg):
+ os.unlink(egg)
+ else:
+ if setuptools.__version__ == '0.0.1':
+ # tell the user to uninstall obsolete version
+ use_setuptools(version)
+
+ req = "setuptools>="+version
+ import pkg_resources
+ try:
+ pkg_resources.require(req)
+ except pkg_resources.VersionConflict:
+ try:
+ from setuptools.command.easy_install import main
+ except ImportError:
+ from easy_install import main
+ main(list(argv)+[download_setuptools(delay=0)])
+ sys.exit(0) # try to force an exit
+ else:
+ if argv:
+ from setuptools.command.easy_install import main
+ main(argv)
+ else:
+ print "Setuptools version",version,"or greater has been installed."
+ print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
+
+
+
+def update_md5(filenames):
+ """Update our built-in md5 registry"""
+
+ import re
+ from md5 import md5
+
+ for name in filenames:
+ base = os.path.basename(name)
+ f = open(name,'rb')
+ md5_data[base] = md5(f.read()).hexdigest()
+ f.close()
+
+ data = [" %r: %r,\n" % it for it in md5_data.items()]
+ data.sort()
+ repl = "".join(data)
+
+ import inspect
+ srcfile = inspect.getsourcefile(sys.modules[__name__])
+ f = open(srcfile, 'rb'); src = f.read(); f.close()
+
+ match = re.search("\nmd5_data = {\n([^}]+)}", src)
+ if not match:
+ print >>sys.stderr, "Internal error!"
+ sys.exit(2)
+
+ src = src[:match.start(1)] + repl + src[match.end(1):]
+ f = open(srcfile,'w')
+ f.write(src)
+ f.close()
+
+
+if __name__=='__main__':
+ if len(sys.argv)>2 and sys.argv[1]=='--md5update':
+ update_md5(sys.argv[2:])
+ else:
+ main(sys.argv[1:])
+
+
+
+
+
View
14 replot.py
@@ -0,0 +1,14 @@
+#!/usr/bin/python
+
+from speed_test import *
+
+if len(sys.argv) == 1:
+ for k in timing_d:
+ plot(k, True)
+ plot(k, False)
+ html(k)
+else:
+ name = sys.argv[1]
+ plot(name, True)
+ plot(name, False)
+ html(name)
View
2  setup.py
@@ -6,7 +6,7 @@
from setuptools import setup, Extension
setup(name='blist',
- version='0.9.2',
+ version='0.9.3',
description='a list-like type with better asymptotic performance',
author='Stutzbach Enterprises, LLC',
author_email='daniel@stutzbachenterprises.com',
View
341 speed_test.py
@@ -0,0 +1,341 @@
+#!/usr/bin/python2.5
+
+import os, sys, subprocess
+from math import *
+
+MIN_REPS = 3
+MIN_TIME = 0.01
+MAX_TIME = 1.0
+
+try:
+ os.mkdir('fig/relative')
+except OSError:
+ pass
+try:
+ os.mkdir('fig/absolute')
+except OSError:
+ pass
+
+make_cache = set()
+current_limit = None
+def make(limit):
+ global current_limit
+ current_limit = limit
+ if limit == 'list': return
+ if limit in make_cache:
+ os.system('cp .cache/blist.so-%d blist.so' % limit)
+ return
+ if os.system('make clean > /dev/null 2> /dev/null'):
+ raise 'Make failure'
+ if os.system('make "COPT=-O3 -DLIMIT=%d -DNDEBUG=1" > /dev/null 2> /dev/null' % limit):
+ raise 'Make failure'
+ os.system('cp blist.so .cache/blist.so-%d' % limit)
+ make_cache.add(limit)
+
+setup = 'from blist import blist'
+
+types = ('blist', 'list')
+ns = range(1,10) + range(10, 100, 10) + range(100, 1000, 100) \
+ + range(1000, 10001, 1000)
+#limits = (8, 64, 128, 512, 2048)
+#limits = (8, 128)
+limits = (128, )
+
+def smart_timeit(stmt, setup, hint):
+ n = hint
+ while 1:
+ v = timeit(stmt, setup, n)
+ if v*n > MIN_TIME:
+ return v, n
+ n <<= 1
+
+timeit_cache = {}
+def timeit(stmt, setup, rep):
+ assert rep >= MIN_REPS
+ key = (stmt, setup, rep, current_limit)
+ if key in timeit_cache:
+ return timeit_cache[key]
+ try:
+ p = subprocess.Popen(['python2.5', '/usr/lib/python2.5/timeit.py',
+ '-r', '5', '-n', str(rep), '-s', setup, stmt],
+ stdout=subprocess.PIPE)
+ so, se = p.communicate()
+ parts = so.split()
+ v = float(parts[-4])
+ units = parts[-3]
+ if units == 'usec':
+ v *= 10.0**-6
+ elif units == 'msec':
+ v *= 10.0**-3
+ elif units == 'sec':
+ pass
+ else:
+ raise 'Unknown units'
+ timeit_cache[key] = v
+ return v
+ except:
+ print stmt
+ print setup
+ raise
+
+values = {}
+def get_timing1(limit, label, setup_n, template, typename, use_rep_map):
+ f = open('dat/%s-%s.dat' % (str(limit), label), 'w')
+ print >>f, '#', label
+ print >>f, '#', template.replace('\n', '\\n')
+ for i in reversed(range(len(ns))):
+ n = ns[i]
+ key = (limit, label, setup_n, n, template, current_limit)
+ print n,
+ sys.stdout.flush()
+ setup2 = '\nTypeToTest = %s\nn = %d\n' % (typename, n)
+ if setup_n is None:
+ setup3 = "x = TypeToTest(range(n))"
+ else:
+ setup3 = setup_n
+ setup3 = setup + '\n' + setup2 + setup3
+ stmt = template
+ if not use_rep_map:
+ if i < len(ns)-1:
+ rep_map[n] = max(rep_map[n], rep_map[ns[i+1]])
+ v, rep = smart_timeit(stmt, setup3, rep_map[n])
+ if rep_map[n] < rep:
+ rep_map[n] = rep
+ else:
+ k = rep_map[n]
+ if k * values[key] > MAX_TIME:
+ k = max(MIN_REPS, int(ceil(MAX_TIME / values[key])))
+ v = timeit(stmt, setup3, k)
+ values[key] = v
+ v *= 1000
+ if limit == 'list':
+ list_values[n] = v
+ print >>f, n, v
+ else:
+ print >>f, n, v, v/list_values[n]
+ print
+ f.close()
+
+def get_timing(label, setup_n, template):
+ global rep_map, list_values
+ rep_map = {}
+ list_values = {}
+ for n in ns:
+ rep_map[n] = MIN_REPS
+ make('list')
+ get_timing1('list', label, setup_n, template, 'list', False)
+ for limit in limits:
+ print 'Timing', label, limit, ':',
+ sys.stdout.flush()
+ make(limit)
+ get_timing1(limit, label, setup_n, template, 'blist', False)
+
+ make('list')
+ get_timing1('list', label, setup_n, template, 'list', True)
+ for limit in limits:
+ print 'Timing', label, limit, ':',
+ sys.stdout.flush()
+ make(limit)
+ get_timing1(limit, label, setup_n, template, 'blist', True)
+
+ plot(label, True)
+ plot(label, False)
+ html(label)
+
+def html(label):
+ fname = 'fig/%s.html' % label
+ f = open(fname, 'w')
+ if timing_d[label][0] is None:
+ setup = 'x = TypeToTest(range(n))'
+ else:
+ setup = timing_d[label][0]
+ print >>f, """
+<html>
+<body>
+<img src="absolute/%s.png"/>
+<img src="relative/%s.png"/>
+<p>
+Setup:
+<pre>
+%s
+</pre>
+Timed:
+<pre>
+%s
+</pre>
+</body>
+</html>
+ """ % (label, label, setup, timing_d[label][1])
+ f.close()
+
+def plot(label, relative):
+ safe_label = label.replace('_', '\\\\_')
+ fname = 'gnuplot/%s.gnuplot' % label
+ f = open(fname, 'w')
+ if relative:
+ d = 'fig/relative/'
+ else:
+ d = 'fig/absolute/'
+ os.putenv('GDFONTPATH', '/usr/share/fonts/truetype/msttcorefonts/')
+ print >>f, """
+set output "%s/%s.png"
+set xlabel "List Size (n)"
+set title "%s"
+set bmargin 3
+
+#set pointsize 2
+#set view 60, 30, 1.0, 1.0
+#set lmargin 12
+#set rmargin 10
+#set tmargin 1
+#set bmargin 5
+#set ylabel 0
+#set mxtics default
+#set mytics default
+#set tics out
+#set nox2tics
+#set noy2tics
+#set border 3
+#set xtics nomirror autofreq
+#set ytics nomirror autofreq
+#set key height 1
+#set nokey
+#unset xdata
+#unset y2label
+#unset x2label
+
+#set format "%%g"
+set terminal png transparent interlace medium font "./times.ttf" size 640,480 nocrop enhanced xffffff x000000 xff0000 x0000ff xc030c0 xff0000 x000000
+set size noratio 1,1
+
+set key below height 1
+""" % (d, label, safe_label)
+
+ if relative:
+ print >>f, 'set title "Normalized Execution Times, log-linear scale"'
+ print >>f, 'set logscale x'
+ print >>f, 'set yrange [0:*]'
+ print >>f, 'set yrange [0:200]'
+ print >>f, 'set ylabel "Execution Time (%)"'
+ print >>f, 'set key bottom left'
+ print >>f, 'set mytics 5'
+ print >>f, 'plot 100 title "list()" ',
+ else:
+ print >>f, 'set title "Raw Execution Times, log-log scale"'
+ print >>f, 'set key top left'
+ print >>f, 'set mytics 10'
+ print >>f, 'set logscale xy'
+ print >>f, 'set yrange [0.0001:10]'
+ print >>f, 'set ylabel "Execution Time"'
+ print >>f, 'set ytics ("100 ns" 0.0001, "1 us" 0.001, "10 us" 0.01, "100 us" 0.1, "1 ms" 1.0, "10 ms" 10.0, "100 ms" 100.0)'
+ print >>f, 'plot "dat/list-%s.dat" title "list()" with linespoints ' \
+ % (label),
+ for limit in limits:
+ print >>f, ', \\'
+ if relative:
+ print >>f, ' "dat/%d-%s.dat" using 1:(100.0*$3) title "blist(), limit=%d" with linespoints' \
+ % (limit, label, limit),
+ else:
+ print >>f, ' "dat/%d-%s.dat" using 1:($2) title "blist(), limit=%d" with linespoints' \
+ % (limit, label, limit),
+ print >>f
+ f.flush()
+ f.close()
+ if os.system('gnuplot "%s"' % fname):
+ raise 'Gnuplot failure'
+
+timing_d = {}
+def add_timing(name, auto, stmt):
+ timing_d[name] = (auto, stmt)
+
+def run_timing(name):
+ auto, stmt = timing_d[name]
+ get_timing(name, auto, stmt)
+
+def run_all():
+ for k in sorted(timing_d):
+ run_timing(k)
+
+#add_timing('insert(0, 0)', False, """\
+#x = %(t)s()
+#for i in xrange(%(n)d):
+# x.insert(0, 0)
+#""")
+
+add_timing('FIFO', None, """\
+x.insert(0, 0)
+del x[0]
+""")
+
+#add_timing('append(0)', False, """\
+#x = %(t)s()
+#for i in xrange(%(n)d):
+# x.append(0)
+#""")
+
+add_timing('LIFO', None, """\
+x.append(0)
+del x[-1]
+""")
+
+add_timing('add', None, """\
+x + x
+""")
+
+add_timing('contains', None, """\
+x.__contains__(-1)
+""")
+
+#add_timing('__delitem__(0)', False, """\
+#x = %(t)s(range(%(n)d))
+#for i in xrange(%(n)d):
+# del x[0]
+#""")
+
+#add_timing('__delitem__(-1)', False, """\
+#x = %(t)s(range(%(n)d))
+#for i in xrange(%(n)d):
+# del x[-1]
+#""")
+
+add_timing('getitem1', None, """\
+x[0]
+""")
+
+add_timing('getitem2', None, """\
+x.__getitem__(0)
+""")
+
+add_timing('getslice', None, """\
+x[1:-1]
+""")
+
+add_timing('forloop', None, "for i in x:\n pass")
+#add_timing('len', None, "len(x)")
+add_timing('eq', None, "x == x")
+add_timing('mul10', None, "x * 10")
+#add_timing('repr', None, "repr(x)")
+add_timing('setitem', None, 'x.__setitem__(0, 1)')
+add_timing('count', None, 'x.count(5)')
+add_timing('reverse', None, 'x.reverse()')
+add_timing('delslice', None, 'del x[len(x)//4:3*len(x)//4]\nx *= 2')
+add_timing('setslice', None, 'x[:] = x')
+#add_timing('index', None, 'x.index(-1)')
+
+add_timing('sort random', 'import random\nx = [random.random() for i in range(n)]', 'y = TypeToTest(x)\ny.sort()')
+add_timing('sort sorted', None, 'y = TypeToTest(x)\ny.sort()')
+add_timing('sort reversed', 'x = range(n)\nx.reverse()', 'y = TypeToTest(x)\ny.sort()')
+
+add_timing('init from list', 'x = range(n)', 'y = TypeToTest(x)')
+add_timing('init from tuple', 'x = tuple(range(n))', 'y = TypeToTest(x)')
+add_timing('init from iterable', 'x = xrange(n)', 'y = TypeToTest(x)')
+
+add_timing('shuffle', 'from random import shuffle\nx = TypeToTest(range(n))', 'shuffle(x)')
+
+if __name__ == '__main__':
+ make(limits[0])
+ if len(sys.argv) == 1:
+ run_all()
+ else:
+ for name in sys.argv[1:]:
+ run_timing(name)
Please sign in to comment.
Something went wrong with that request. Please try again.