summaryrefslogtreecommitdiff
path: root/benchmarks/benchmarks/10_registry.py
blob: 41da67b3437046e298267c66bb674d81d6b1d11b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pathlib

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"))
]


def time_parse_math_expression():
    ureg.parse_expression("3 + 5 * 2 + value", value=10)


# This code is duplicated with other benchmarks but simplify comparison

CACHE_FOLDER = pathlib.Path(".cache")
CACHE_FOLDER.mkdir(exist_ok=True)
pint.UnitRegistry(cache_folder=CACHE_FOLDER)


def time_load_definitions_stage_1(cache_folder):
    """empty registry creation"""
    # Change this into a single part benchmark using setup
    _ = pint.UnitRegistry(None, cache_folder=None)


time_load_definitions_stage_1.param_names = [
    "cache_folder",
]
time_load_definitions_stage_1.params = [
    None,
    CACHE_FOLDER,
]


def time_load_definitions_stage_2(cache_folder, *args, **kwargs):
    """empty registry creation + parsing default files + definition object loading"""

    # Change this into a single part benchmark using setup
    empty_registry = pint.UnitRegistry(None, cache_folder=cache_folder)
    empty_registry.load_definitions("default_en.txt", True)


time_load_definitions_stage_2.param_names = time_load_definitions_stage_1.param_names
time_load_definitions_stage_2.params = time_load_definitions_stage_1.params


def time_load_definitions_stage_3(cache_folder, *args, **kwargs):
    """empty registry creation + parsing default files + definition object loading + cache building"""

    # Change this into a single part benchmark using setup
    empty_registry = pint.UnitRegistry(None, cache_folder=cache_folder)
    loaded_files = empty_registry.load_definitions("default_en.txt", True)
    empty_registry._build_cache(loaded_files)


time_load_definitions_stage_3.param_names = time_load_definitions_stage_1.param_names
time_load_definitions_stage_3.params = time_load_definitions_stage_1.params