File: if_vs_dict_lookup.py

package info (click to toggle)
python-pyperform 1.86-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 168 kB
  • sloc: python: 729; makefile: 17
file content (34 lines) | stat: -rw-r--r-- 740 bytes parent folder | download
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
__author__ = 'calvin'
import random #!

from pyperform import ComparisonBenchmark


def _setup():
    l = []
    lookup = {ii: l.append for ii in range(5)}


largs = (random.randint(0, 4),)

@ComparisonBenchmark('group1', setup=_setup, validation=True, timeit_number=10, timeit_repeat=1000)
def do_if():
    ii = random.randint(0, 4)
    if ii == 0:
        l.append(ii)
    elif ii == 1:
        l.append(ii)
    elif ii == 2:
        l.append(ii)
    elif ii == 3:
        l.append(ii)
    elif ii == 4:
        l.append(ii)

@ComparisonBenchmark('group1', setup=_setup, validation=True, timeit_number=10, timeit_repeat=1000)
def do_dict_lookup():
    ii = random.randint(0, 4)
    lookup[ii](ii)


ComparisonBenchmark.summarize('group1')