-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_benchmark.py
65 lines (46 loc) · 1.85 KB
/
unit_benchmark.py
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
from unitexpr.si_units import m, s, kg, N, SiUnit
from scimath.units.length import meter
from scimath.units.time import second
from scimath.units.mass import kilogram
v = SiUnit("v", "meter/second", "speed", expr=10.0 * m / s)
# Expressions
v_expr = v * 1.0
one = SiUnit("one", "one", "integer", SiUnit.expr_type.one)
ten = SiUnit("ten", "ten", "integer", 10.0 * SiUnit.expr_type.one)
seven = SiUnit("seven", "seven", "integer", 7 * SiUnit.expr_type.one)
zero = SiUnit("zero", "zero", "integer", one * 0.0)
v_scimath = meter / second
N_scimath = kilogram * meter / (second * second)
class TestAddition:
def test_add_unitexpr_units(self, benchmark):
def add():
return m / s + m / s
benchmark.pedantic(add, iterations=20000, rounds=4)
assert add() == v / 5.0
def test_add_scimath_units(self, benchmark):
def add():
return meter / second + meter / second
benchmark.pedantic(add, iterations=20000, rounds=4)
assert add() == 2.0 * meter / second
class TestComparison:
def test_compare_unitexpr_units(self, benchmark):
def compare():
return v == N
benchmark.pedantic(compare, iterations=20000, rounds=4)
assert v == v
def test_compare_scimath_units(self, benchmark):
def compare():
return v_scimath == N_scimath
benchmark.pedantic(compare, iterations=20000, rounds=4)
assert v_scimath == v_scimath
class TestMul:
def test_mult_unitexpr_units(self, benchmark):
def expr():
return kg * m * s ** -2
benchmark.pedantic(expr, iterations=20000, rounds=4)
assert expr() == N
def test_mult_scimath_units(self, benchmark):
def expr():
return kilogram * meter * second ** -2
benchmark.pedantic(expr, iterations=20000, rounds=4)
assert expr() == N_scimath