Skip to content

Instantly share code, notes, and snippets.

@paulocheque
Last active August 29, 2015 14:05
Show Gist options
  • Save paulocheque/7f7ea9642b500359acee to your computer and use it in GitHub Desktop.
Save paulocheque/7f7ea9642b500359acee to your computer and use it in GitHub Desktop.
benchmarks
import json
import re
import timeit
import ujson
import simplejson
# from memory_profiler import profile
# @profile(precision=4)
def format_result(stmt, r):
print('%10s %.5f' % (stmt, min(r)))
def bench(stmt, setup, n=10000, repeat=10):
r = timeit.repeat(stmt, setup=setup, number=n, repeat=repeat)
format_result(stmt, r)
class A(object):
@staticmethod
def a():
return None
@classmethod
def b(cls):
return None
def c(self):
return None
def a():
return None
def b(a):
return None
c = lambda:None
J = dict([(i, v) for i, v in enumerate(range(100))])
def d():
json.dumps(J)
def e(): # fatest
ujson.dumps(J)
def f():
simplejson.dumps(J)
def g():
a = '/group/1234567890/user/1234567890/'
t = a.split('/')
v = t[0] == '/group' and t[1] == '/user/'
# print(v)
return v
def h(): # slowest
a = '/group/1234567890/user/1234567890/'
v = re.match('/group/\d+/user/\d+/', a)
# print(v)
return v
x = re.compile('/group/\d+/user/\d+/')
def i():
a = '/group/1234567890/user/1234567890/'
v = x.match(a)
# print(v)
return v
if __name__ == "__main__":
setup = 'from __main__ import A, a, b, c, J, d, e, f, g, h, i'
print('Testing class methods')
bench("A.a()", setup)
bench("A.b()", setup)
bench("A().c()", setup)
print('Testing functions')
bench("a()", setup)
bench("b(1)", setup)
bench("c()", setup)
print('Testing json libs')
bench("d()", setup)
bench("e()", setup)
bench("f()", setup)
print('Strings vs regex')
bench("g()", setup)
bench("h()", setup)
bench("i()", setup)
python2.7 -m timeit "a = lambda:None; [a() for i in range(100)]"
python3.4 -m timeit "a = lambda:None; [a() for i in range(100)]"
pypy -m timeit "a = lambda:None; [a() for i in range(100)]"
python2.7 -m timeit "A = type('A', (), {}); [A() for i in range(100)]"
python3.4 -m timeit "A = type('A', (), {}); [A() for i in range(100)]"
pypy -m timeit "A = type('A', (), {}); [A() for i in range(100)]"
python2.7 -m timeit "A = type('A', (), {'a':lambda x:None}); a = A(); [a.a() for i in range(100)]"
python3.4 -m timeit "A = type('A', (), {'a':lambda x:None}); a = A(); [a.a() for i in range(100)]"
pypy -m timeit "A = type('A', (), {'a':lambda x:None}); a = A(); [a.a() for i in range(100)]"
python2.7 -m timeit "A = type('A', (), {}); A.a = lambda x:None; a = A(); [a.a() for i in range(100)]"
python3.4 -m timeit "A = type('A', (), {}); A.a = lambda x:None; a = A(); [a.a() for i in range(100)]"
pypy -m timeit "A = type('A', (), {}); A.a = lambda x:None; a = A(); [a.a() for i in range(100)]"
# results in seconds
ruby -e "require 'benchmark'; time = Benchmark.measure do 10000.times { (0..100).to_a.join('-') } end; puts time;"
# results in micro seconds: seconds = micro second / 1000000
python2.7 -m timeit '"-".join(str(n) for n in range(100))'
python2.7 -m timeit '"-".join(str(n) for n in xrange(100))'
python3.4 -m timeit '"-".join(str(n) for n in range(100))'
pypy -m timeit '"-".join(str(n) for n in range(100))'
memory_profiler
simplejson
ujson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment