Skip to content

Instantly share code, notes, and snippets.

@ifduyue
Created December 12, 2011 07:40
Show Gist options
  • Save ifduyue/1465705 to your computer and use it in GitHub Desktop.
Save ifduyue/1465705 to your computer and use it in GitHub Desktop.
benchmark pylru, lrucache, lru
import lru
import lrucache
import pylru
import random
import sys
size = int(sys.argv[1])
data = []
range_ = int(sys.argv[2])
for i in xrange(int(sys.argv[3])):
data.append(random.randint(0, range_))
def test_lru():
c = lru.LRUCache(size)
for i in data:
c[i] = i
def test_pylru():
c = pylru.lrucache(size)
for i in data:
c[i] = i
def test_lrucache():
c = lrucache.LRUCache(size)
for i in data:
c[i] = i
import timeit
t1 = timeit.Timer("test_pylru()", "from __main__ import test_pylru")
t2 = timeit.Timer("test_lrucache()", "from __main__ import test_lrucache")
t3 = timeit.Timer("test_lru()", "from __main__ import test_lru")
print t1.timeit(5), 'pylru'
print t2.timeit(5), 'lrucache'
print t3.timeit(5), 'lru'
$ python benchmark_pylru_lrucache.py 10 1000 1000
0.00981712341309 pylru
0.0572061538696 lrucache
0.0165300369263 lru
$ python benchmark_pylru_lrucache.py 1000 1000 1000
0.0181090831757 pylru
1.9328930378 lrucache
0.0636470317841 lru
$ python benchmark_pylru_lrucache.py 1000 1000000 20000
0.199607133865 pylru
2.25100207329 lrucache
5.86161112785 lru
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment