From 4aca866c292eec899f75475ff14f7d5c1025e394 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Wed, 15 Feb 2023 16:59:07 +0100 Subject: ENH: Improve performance of finfo and _commonType (#23088) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The finfo contains a cache for dtypes, but the np.complex128 dtype does not end up in the cache. The reason is that the np.complex128 is converted to np.float64 which is in the cache. Performance improvement for finfo(np.complex128): Main: 2.07 µs ± 75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) Pr: 324 ns ± 28.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) Improve performance of finfo by making the cache check the first action in the __new__ Improve performance of _commonType by re-using the expression for a.dtype.type and eliminating variables The finfo and _commonType was part of the computatation time in lstsq when using scikit-rf. Since these methods are used in various other methods performance can improve there slightly as well. --- numpy/linalg/linalg.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'numpy/linalg') diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 92878228d..255de94e5 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -138,24 +138,24 @@ def _commonType(*arrays): result_type = single is_complex = False for a in arrays: - if issubclass(a.dtype.type, inexact): - if isComplexType(a.dtype.type): + type_ = a.dtype.type + if issubclass(type_, inexact): + if isComplexType(type_): is_complex = True - rt = _realType(a.dtype.type, default=None) - if rt is None: + rt = _realType(type_, default=None) + if rt is double: + result_type = double + elif rt is None: # unsupported inexact scalar raise TypeError("array type %s is unsupported in linalg" % (a.dtype.name,)) else: - rt = double - if rt is double: result_type = double if is_complex: - t = cdouble result_type = _complex_types_map[result_type] + return cdouble, result_type else: - t = double - return t, result_type + return double, result_type def _to_native_byte_order(*arrays): -- cgit v1.2.1