summaryrefslogtreecommitdiff
path: root/benchmarks/10_registry.py
blob: d95a53fce387451c7fac967f47aba64633782cb6 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pint

from . import util

units = ("meter", "kilometer", "second", "minute", "angstrom")

other_units = ("meter", "angstrom", "kilometer/second", "angstrom/minute")

all_values = ("int", "float", "complex")

ureg = None
data = {}


def setup(*args):

    global ureg, data

    data["int"] = 1
    data["float"] = 1.0
    data["complex"] = complex(1, 2)

    ureg = pint.UnitRegistry(util.get_tiny_def())


def my_setup(*args):
    global data
    setup(*args)
    for unit in units + other_units:
        data["uc_%s" % unit] = pint.registry.to_units_container(unit, ureg)


def time_build_cache():
    ureg._build_cache()


def time_getattr(key):
    getattr(ureg, key)


time_getattr.params = units


def time_getitem(key):
    ureg[key]


time_getitem.params = units


def time_parse_unit_name(key):
    ureg.parse_unit_name(key)


time_parse_unit_name.params = units


def time_parse_units(key):
    ureg.parse_units(key)


time_parse_units.params = units


def time_parse_expression(key):
    ureg.parse_expression("1.0 " + key)


time_parse_expression.params = units


def time_base_units(unit):
    ureg.get_base_units(unit)


time_base_units.params = other_units


def time_to_units_container_registry(unit):
    pint.registry.to_units_container(unit, ureg)


time_to_units_container_registry.params = other_units


def time_to_units_container_detached(unit):
    pint.registry.to_units_container(unit, ureg)


time_to_units_container_detached.params = other_units


def time_convert_from_uc(key):
    src, dst = key
    ureg._convert(1.0, data[src], data[dst])


time_convert_from_uc.setup = my_setup
time_convert_from_uc.params = [
    (("uc_meter", "uc_kilometer"), ("uc_kilometer/second", "uc_angstrom/minute"))
]