File: bench_dict.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (52 lines) | stat: -rw-r--r-- 1,427 bytes parent folder | download | duplicates (10)
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

""" some simple benchmarikng stuff
"""

import random, time

def sample(population, num):
    l = len(population)
    retval = []
    for i in xrange(num):
        retval.append(population[random.randrange(l)])
    return retval

random.sample = sample

def get_random_string(l):
    strings = 'qwertyuiopasdfghjklzxcvbm,./;QWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*()_+1234567890-='
    return str(random.sample(strings, l))

def count_operation(name, function):
    print name
    t0 = time.time()
    retval = function()
    tk = time.time()
    print name, " takes: %f" % (tk - t0)
    return retval

def bench_simple_dict(SIZE = 10000):
    keys = [get_random_string(20) for i in xrange(SIZE)]
    values = [random.random() for i in xrange(SIZE)]

    lookup_keys = random.sample(keys, 1000)
    random_keys = [get_random_string(20) for i in xrange(1000)]
    
    test_d = count_operation("Creation", lambda : dict(zip(keys, values)))

    def rand_keys(keys):
        for key in keys:
            try:
                test_d[key]
            except KeyError:
                pass
    
    count_operation("Random key access", lambda : rand_keys(random_keys))
    count_operation("Existing key access", lambda : rand_keys(lookup_keys))
    return test_d

if __name__ == '__main__':
    test_d = bench_simple_dict()
    import __pypy__
    print __pypy__.internal_repr(test_d)
    print __pypy__.internal_repr(test_d.iterkeys())