1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
import sys
import os
from xml.dom.minidom import getDOMImplementation
from timeit import Timer
import subprocess
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', 'build', 'default', 'benchmarks'))
TIMES = 10000000
TIMES1 = TIMES/4
import testapi_pybindgen
import testapi_boost
import testapi_swig
import testapi_sip
def bench(mod, dom, elem):
def bench():
return mod.func1()
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call function with no arguments")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
def bench():
return mod.func2(1.0, 2.0, 3.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call function taking 3 doubles")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
def bench():
return mod.Multiplier()
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call class constructor with no arguments")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
def bench():
return mod.Multiplier(3.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call class constructor with double")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
obj = mod.Multiplier(3.0)
def bench():
return obj.GetFactor()
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call simple method")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
obj = mod.Multiplier(3.0)
def bench():
return obj.SetFactor()
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call overloaded method 1")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
obj = mod.Multiplier(3.0)
def bench():
return obj.SetFactor(1.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call overloaded method 2")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
obj = mod.Multiplier(3.0)
def bench():
return obj.Multiply(5.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call non-overridden virtual method with double")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
class M(mod.Multiplier):
def Multiply(self, value):
return super(M, self).Multiply(value)
obj = M(2.0)
def bench():
return obj.Multiply(5.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call python-overridden virtual method from Python")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES1)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
def bench():
return mod.call_virtual_from_cpp(obj, 5.0)
tst = elem.appendChild(dom.createElement('test'))
tst.setAttribute("description", "call python-overridden virtual method from C++")
tst.setAttribute("time", repr(Timer(bench).timeit(TIMES1)))
print "%s (%s): %s" % (tst.tagName, tst.getAttribute('description'), tst.getAttribute('time'))
def main():
impl = getDOMImplementation()
dom = impl.createDocument(None, "pybindgen-benchmarks", None)
top = dom.documentElement
env = top.appendChild(dom.createElement('environment'))
env.appendChild(dom.createElement('compiler')).appendChild(dom.createTextNode(
subprocess.Popen(["g++", "-v"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]))
env.appendChild(dom.createElement('python')).appendChild(dom.createTextNode(
sys.version))
env.appendChild(dom.createElement('swig')).appendChild(dom.createTextNode(
subprocess.Popen(["swig", "-version"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]))
env.appendChild(dom.createElement('boost_python')).appendChild(dom.createTextNode(
subprocess.Popen(["dpkg", "-s", "libboost-python-dev"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]))
env.appendChild(dom.createElement('pybindgen')).appendChild(dom.createTextNode(
subprocess.Popen(["bzr", "version-info", '--check-clean'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]))
env.appendChild(dom.createElement('cpu')).appendChild(dom.createTextNode(
file("/proc/cpuinfo").read()))
if len(sys.argv) == 3:
env.appendChild(dom.createElement('CXXFLAGS')).appendChild(dom.createTextNode(sys.argv[2]))
res = top.appendChild(dom.createElement('results'))
print "pybindgen results:"
pbg = res.appendChild(dom.createElement('pybindgen'))
pbg.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_pybindgen.so").st_size))
bench(testapi_pybindgen, dom, pbg)
print "boost_python results:"
bp = res.appendChild(dom.createElement('boost_python'))
bp.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_boost.so").st_size))
bench(testapi_boost, dom, bp)
print "swig results:"
sw = res.appendChild(dom.createElement('swig'))
sw.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/_testapi_swig.so").st_size))
sw.setAttribute("module-python-file-size", repr(os.stat("build/default/benchmarks/testapi_swig.py").st_size))
bench(testapi_swig, dom, sw)
print "sip results:"
sip = res.appendChild(dom.createElement('sip'))
sip.setAttribute("module-file-size", repr(os.stat("build/default/benchmarks/testapi_sip.so").st_size))
bench(testapi_sip, dom, sip)
if len(sys.argv) == 3:
f = open(sys.argv[1], "wb")
dom.writexml(f, "", " ", "\n")
f.close()
if __name__ == '__main__':
main()
|