def test_speed_parse_and_convert(): print() ureg = uc.getGlobalUnitRegistry() q = ureg.makeQuantity('100 W') pureg = pint.UnitRegistry() pq = pureg.Quantity(100, 'W') bm = Benchmark() bm.run(lambda: pureg.Quantity("100 m").to("cm")) pint_runtime = bm.measurement bm.run(lambda: ureg.makeQuantity("100 m").to("cm")) uc_runtime = bm.measurement print("parse and convert: 100 m -> cm") print("Pint", pint_runtime) print("UnitConvert", uc_runtime) print("Speedup", Speedup(pint_runtime, uc_runtime)) print() print() bm = Benchmark() bm.run(lambda: pureg.Quantity("100 W").to("g cm mm / hour / min / ms")) pint_runtime = bm.measurement bm.run(lambda: ureg.makeQuantity("100 W").to("g cm mm / hour / min / ms")) uc_runtime = bm.measurement print("parse and convert: 100 W -> g cm mm / hr / min / ms") print("Pint", pint_runtime) print("UnitConvert", uc_runtime) print("Speedup", Speedup(pint_runtime, uc_runtime))
def test_global_registry(): ureg = uc.getGlobalUnitRegistry() q = ureg.makeQuantity('1 m') print(type(q)) q2 = q.to("cm") print(type(q2)) assert q2.value() == Approx(100) q = ureg.makeQuantity('100 mph') q2 = q.to("m/s") assert q2.value() == Approx(44.704)
def test_speed_simple(): ureg = uc.getGlobalUnitRegistry() q = ureg.makeQuantity('100 mile/hour') pureg = pint.UnitRegistry() pq = pureg.Quantity(100, 'mile/hour') bm = Benchmark() bm.run(lambda: q.to("km/s")) uc_runtime = bm.measurement bm.run(lambda: pq.to("km/s")) pint_runtime = bm.measurement print("convert: 100 mph -> km/s") print("Pint", pint_runtime) print("UnitConvert", uc_runtime) print("Speedup", Speedup(pint_runtime, uc_runtime))
def test_pint_calc_with_uc_conversion(): print() ureg = uc.getGlobalUnitRegistry() pureg = pint.UnitRegistry() l = pureg.Quantity(100, 'in') w = pureg.Quantity(100, 'cm') A = l * w bm = Benchmark() bm.run(lambda: A.to("acre")) pint_runtime = bm.measurement bm.run(lambda: pureg.Quantity( ureg.makeQuantity(str(A)).to("acre").value(), "acre")) uc_runtime = bm.measurement print("pint calc with uc conversion") print("Pint", pint_runtime) print("UnitConvert", uc_runtime) print("Speedup", Speedup(pint_runtime, uc_runtime))