From 8acb0f7e73be9b8e797ffbe1bdce4d7076202183 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 18 Sep 2017 10:04:37 -0500 Subject: MAINT: When delta is a NumPy scalar, do multiplication in-place This is faster and more memory efficient. Out-of-place operation was preventing me from using linspace to create over 2**31 equispaced doubles on my machine due to insufficient memory. --- numpy/core/function_base.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'numpy/core/function_base.py') diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 0415e16ac..82de1a36e 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -115,17 +115,24 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): y = _nx.arange(0, num, dtype=dt) delta = stop - start + # In-place multiplication y *= delta/div is faster, but prevents the multiplicant + # from overriding what class is produced, and thus prevents, e.g. use of Quantities, + # see gh-7142. Hence, we multiply in place only for standard scalar types. + _mult_inplace = _nx.isscalar(delta) if num > 1: step = delta / div if step == 0: # Special handling for denormal numbers, gh-5437 y /= div - y = y * delta + if _mult_inplace: + y *= delta + else: + y = y * delta else: - # One might be tempted to use faster, in-place multiplication here, - # but this prevents step from overriding what class is produced, - # and thus prevents, e.g., use of Quantities; see gh-7142. - y = y * step + if _mult_inplace: + y *= step + else: + y = y * step else: # 0 and 1 item long sequences have an undefined step step = NaN -- cgit v1.2.1