Created
June 12, 2014 05:31
-
-
Save moea/be38aa931ec2110bd669 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, geohash, guppy, itertools, time | |
def randomHash(): | |
lat = (random.random() * 180) - 90 | |
lng = (random.random() * 360) - 180 | |
return geohash.encode(lat, lng) | |
def insert(d, hash, userId): | |
for c in hash[:11]: | |
d = d.setdefault(c, {}) | |
last = hash[11] | |
d.setdefault(last, set()).add(userId) | |
def lookup(d, hash): | |
for c in hash: | |
if c not in d: | |
return | |
d = d[c] | |
return d | |
def timeme(func): | |
def wrapper(*arg,**kw): | |
t1 = time.time() | |
res = func(*arg,**kw) | |
t2 = time.time() | |
return (t2-t1), res | |
return wrapper | |
if __name__ == '__main__': | |
h = guppy.hpy() | |
h.setrelheap() | |
d = {} | |
ordinals = itertools.count() | |
for _ in xrange(50000): | |
hash = randomHash() | |
insert(d, hash, ordinals.next()) | |
print 'Memory usage for 50k hashes' | |
print h.heap() | |
@timeme | |
def insertMany(hashes): | |
for hash in hashes: | |
insert(d, hash, ordinals.next()) | |
hashes = [randomHash() for _ in xrange(100000)] | |
(msecs, _) = insertMany(hashes) | |
print '100,000 inserts: %.2fs' % (msecs,) | |
@timeme | |
def lookupMany(hashes): | |
for hash in hashes: | |
lookup(d, hash) | |
(msecs, _) = lookupMany(hashes) | |
print '100,000 lookups: %.2fs' % (msecs,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment