From ddf2e1d4556273833c8ac0ebe6b841bdc65ce416 Mon Sep 17 00:00:00 2001 From: Justin Gerber Date: Sun, 29 Dec 2024 04:55:29 +0900 Subject: [PATCH] instance check for ABC real seems to be slow. See if removing that improves performance. --- uncertainties/core.py | 5 ++--- uncertainties/ucombo.py | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/uncertainties/core.py b/uncertainties/core.py index 7fcce28d..db58fe1e 100644 --- a/uncertainties/core.py +++ b/uncertainties/core.py @@ -16,7 +16,6 @@ from builtins import str, zip, range, object from math import isnan, sqrt # Optimization: no attribute look-up -from numbers import Real from typing import Optional, Union from uncertainties.formatting import format_ufloat @@ -286,7 +285,7 @@ class UFloat(object): def __init__( self, nominal_value: float, - uncertainty: Union[UCombo, Real], + uncertainty: Union[UCombo, float], tag: Optional[str] = None, ): """ @@ -302,7 +301,7 @@ def __init__( float such that a new UCombo and UAtom is generated. """ self._nominal_value = float(nominal_value) - if isinstance(uncertainty, Real): + if isinstance(uncertainty, float): if not isnan(uncertainty) and uncertainty < 0: raise NegativeStdDev("The standard deviation cannot be negative") if uncertainty == 0: diff --git a/uncertainties/ucombo.py b/uncertainties/ucombo.py index d9ee49fa..cd7c71ec 100644 --- a/uncertainties/ucombo.py +++ b/uncertainties/ucombo.py @@ -2,7 +2,6 @@ from collections import defaultdict from math import sqrt -from numbers import Real from typing import Optional, Tuple, Union import uuid @@ -92,14 +91,14 @@ def __add__(self: UCombo, other) -> UCombo: def __radd__(self: UCombo, other: UCombo) -> UCombo: return self.__add__(other) - def __mul__(self: UCombo, scalar: Real) -> UCombo: - if not isinstance(scalar, Real): + def __mul__(self: UCombo, scalar: float) -> UCombo: + if not isinstance(scalar, float): return NotImplemented if scalar == 0 or not self: return UCombo(()) return UCombo(((self, float(scalar)),)) - def __rmul__(self: UCombo, scalar: Real) -> UCombo: + def __rmul__(self: UCombo, scalar: float) -> UCombo: return self.__mul__(scalar) def __iter__(self: UCombo):