You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
np.clip(a, mn, mx) on Windows runs slower than np.clip(np.clip(a, None, mx), mn, None)) #18673
On Windows platform, np.clip() on floating point inputs with both bounds provided runs significantly slower than calling np.clip() twice, once with the lower bound, and again with the upper bound.
I noticed that the use of _NPY_CLIP macro would result in multiple re-evaluations of the input arguments.
By avoiding the use of the _NPY_CLIP macro and using a temporary variable, I was able to get np.clip to run faster than myclip in the reproducing example script above. (Windows 10 x64, MSVC 19.28)
file numpy\core\src\umath\clip.c.src
NPY_NO_EXPORTvoid
@name@_clip(char**args, npy_intpconst*dimensions, npy_intpconst*steps, void*NPY_UNUSED(func))
{
if (steps[1] ==0&&steps[2] ==0) {
/* min and max are constant throughout the loop, the most common case *//* NOTE: it may be possible to optimize these checks for nan */
@type@ min_val=*(@type@ *)args[1];
@type@ max_val=*(@type@ *)args[2];
char*ip1=args[0], *op1=args[3];
npy_intpis1=steps[0], os1=steps[3];
npy_intpn=dimensions[0];
/* contiguous, branch to let the compiler optimize */if (is1==sizeof(@type@) &&os1==sizeof(@type@)) {
for(npy_intpi=0; i<n; i++, ip1+=is1, op1+=os1) {
// *(@type@ *)op1 = _NPY_CLIP(*(@type@ *)ip1, min_val, max_val);
@type@ t=*(@type@ *)ip1;
t=_NPY_@name@_MAX(t, min_val);
t=_NPY_@name@_MIN(t, max_val);
*(@type@ *)op1=t;
}
}
else {
for(npy_intpi=0; i<n; i++, ip1+=is1, op1+=os1) {
// *(@type@ *)op1 = _NPY_CLIP(*(@type@ *)ip1, min_val, max_val);
@type@ t=*(@type@ *)ip1;
t=_NPY_@name@_MAX(t, min_val);
t=_NPY_@name@_MIN(t, max_val);
*(@type@ *)op1=t;
}
}
}
else {
TERNARY_LOOP {
*(@type@ *)op1=_NPY_CLIP(*(@type@ *)ip1, *(@type@ *)ip2, *(@type@ *)ip3);
}
}
npy_clear_floatstatus_barrier((char*)dimensions);
}
Activity
rossbar commentedon Mar 24, 2021
I can't replicate this behavior, albeit on linux.
pijyoi commentedon Mar 24, 2021
Maybe I should have specified more clearly that the issue is only for Windows. Yes, on Linux, np.clip is faster, as would be expected.
rossbar commentedon Mar 24, 2021
No you were plenty clear 👍 , I was just surprised by the platform dependence.
rajpratyush commentedon Apr 14, 2021
how about if we use joblib here ?
pijyoi commentedon Oct 19, 2021
I noticed that the use of _NPY_CLIP macro would result in multiple re-evaluations of the input arguments.
By avoiding the use of the _NPY_CLIP macro and using a temporary variable, I was able to get
np.clipto run faster thanmyclipin the reproducing example script above. (Windows 10 x64, MSVC 19.28)file
numpy\core\src\umath\clip.c.srcBUG: fix win32 np.clip slowness
pijyoi commentedon Oct 22, 2021
fixed by #20134