Debian Codegraph @

pypy/objspace/std/benchmark/bench_dict.py

raw symbols SCIP
1
2 """ some simple benchmarikng stuff
3 """
4
5 import random, time
6
7 def sample(population, num):
8 l = len(population)
9 retval = []
10 for i in xrange(num):
11 retval.append(population[random.randrange(l)])
12 return retval
13
14 random.sample = sample
15
16 def get_random_string(l):
17 strings = 'qwertyuiopasdfghjklzxcvbm,./;QWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*()_+1234567890-='
18 return str(random.sample(strings, l))
19
20 def count_operation(name, function):
21 print name
22 t0 = time.time()
23 retval = function()
24 tk = time.time()
25 print name, " takes: %f" % (tk - t0)
26 return retval
27
28 def bench_simple_dict(SIZE = 10000):
29 keys = [get_random_string(20) for i in xrange(SIZE)]
30 values = [random.random() for i in xrange(SIZE)]
31
32 lookup_keys = random.sample(keys, 1000)
33 random_keys = [get_random_string(20) for i in xrange(1000)]
34
35 test_d = count_operation("Creation", lambda : dict(zip(keys, values)))
36
37 def rand_keys(keys):
38 for key in keys:
39 try:
40 test_d[key]
41 except KeyError:
42 pass
43
44 count_operation("Random key access", lambda : rand_keys(random_keys))
45 count_operation("Existing key access", lambda : rand_keys(lookup_keys))
46 return test_d
47
48 if __name__ == '__main__':
49 test_d = bench_simple_dict()
50 import __pypy__
51 print __pypy__.internal_repr(test_d)
52 print __pypy__.internal_repr(test_d.iterkeys())