From aad1c737c34b2bf663017493652838a516ee7a01 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Thu, 28 Sep 2017 09:11:07 -0700 Subject: [PATCH] eq(): better performance by avoiding array comparison when shapes do not match --- pyqtgraph/functions.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 1aed6ace..261727cb 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -417,7 +417,21 @@ def eq(a, b): """ if a is b: return True - + + # Avoid comparing large arrays against scalars; this is expensive and we know it should return False. + aIsArr = isinstance(a, (np.ndarray, MetaArray)) + bIsArr = isinstance(b, (np.ndarray, MetaArray)) + if (aIsArr or bIsArr) and type(a) != type(b): + return False + + # If both inputs are arrays, we can speeed up comparison if shapes / dtypes don't match + # NOTE: arrays of dissimilar type should be considered unequal even if they are numerically + # equal because they may behave differently when computed on. + if aIsArr and bIsArr and (a.shape != b.shape or a.dtype != b.dtype): + return False + + # Test for equivalence. + # If the test raises a recognized exception, then return Falase try: try: # Sometimes running catch_warnings(module=np) generates AttributeError ???