-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance increase rolling min max #19549
Performance increase rolling min max #19549
Conversation
Hello @hexgnu! Thanks for updating the PR.
Comment last updated on February 12, 2018 at 04:38 Hours UTC |
pls add a whatsnew, and I think we have asv's for this, so run and post those as well (of course need passing too :>) approach looks good. |
Codecov Report
@@ Coverage Diff @@
## master #19549 +/- ##
==========================================
+ Coverage 91.58% 91.59% +<.01%
==========================================
Files 150 150
Lines 48807 48807
==========================================
+ Hits 44702 44704 +2
+ Misses 4105 4103 -2
Continue to review full report at Codecov.
|
@@ -1,11 +0,0 @@ | |||
#ifndef _PANDAS_MATH_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't remove this. otherwise windows builds will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you are trying to fix this using the cpp library below. not sure how this will work out. windows is a bit funky on its incudes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea I think I'm going to revert it back to math.h even though it's a tad wonky, it was actually working quite well that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh ugh I just realized I didn't checkin src/headers/cmath which is why this whole thing is failing.
Ok pretty sure this will pass this time since I went through the trouble of setting up windows and testing it on MSVC 9.0. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. just a little docs. ping on green.
@@ -1242,32 +1244,43 @@ cdef _roll_min_max(ndarray[numeric] input, int64_t win, int64_t minp, | |||
|
|||
output = np.empty(N, dtype=input.dtype) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a description of the algorithm and a link (if available)
@@ -0,0 +1,13 @@ | |||
#ifndef _PANDAS_MATH_H_ | |||
#define _PANDAS_MATH_H_ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add commets here on why we have this file (so the next person doesn't go thru the same as you :>)
I'm not sure it matters in practice with the toolchains that actually get used but historically we haven't used c++ at all inside pandas - maybe has implications adding it? |
pandas/_libs/src/headers/cmath
Outdated
#if defined(_MSC_VER) && (_MSC_VER < 1800) | ||
#include <cmath> | ||
namespace std { | ||
__inline int signbit(double num) { return _copysign(1.0, num) < 0; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would revert this to use math.h - technically extending namespace std
is undefined behavior so you'd need to do it a different way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's the right direction to go, cmath is what you use when you write C++ code. math.h doesn't work either when compiling in clang. This works in all three compilers even though it's not the most ideal (MSVC doesn't have this defined for older versions which is annoying).
Here are the ASVs I ran. But I'm noticing 1 an increase in time_pairwise and also idk if there's a variable window rolling benchmark. So I'll look into that.
|
we have other c++ code so this is not a big deal |
@chris-b1 also there is C++ code in the repo look at msgpack |
So in reading this most of the benchmarks for variable rolling windows has gone down but some haven't. How accurate is ASV? Or is it perhaps me doing something else impacting it, cause I was doing some work in a jupyter notebook.
|
can you add a whatsnew note in perf. lgtm otherwise. I suspect that a very short time window has a small perf regression here, but we win really big for bigger windows. So this is a nice tradeoff (in theory we could use the original method for short windows but that adds a lot to code complexity). You could add this in the code as a comment. |
i restarted. ping on green. |
thanks @hexgnu nice patch! |
…21704) User reported that `df.rolling(to_offset('3D'), closed='left').max()` segfaults when df has a datetime index. The bug was in PR pandas-dev#19549. In that PR, in https://github.com/pandas-dev/pandas/blame/master/pandas/_libs/window.pyx#L1268 `i` is initialized to `endi[0]`, which is 0 when `closed=left`. So in the next line when it tries to set `output[i-1]` it goes out of bounds. In addition, there are 2 more bugs in the `roll_min_max` code. The second bug is that for variable size windows, the `nobs` is never updated when elements leave the window. The third bug is at the end of the fixed window where all output elements up to `minp` are initialized to 0 if the input is not float. This PR fixes all three of the aforementioned bugs, at the cost of casting the output array to floating point even if the input is integer. This is less than ideal if the output has no NaNs, but is still consistent with roll_sum behavior.
…21704) User reported that `df.rolling(to_offset('3D'), closed='left').max()` segfaults when df has a datetime index. The bug was in PR pandas-dev#19549. In that PR, in https://github.com/pandas-dev/pandas/blame/master/pandas/_libs/window.pyx#L1268 `i` is initialized to `endi[0]`, which is 0 when `closed=left`. So in the next line when it tries to set `output[i-1]` it goes out of bounds. In addition, there are 2 more bugs in the `roll_min_max` code. The second bug is that for variable size windows, the `nobs` is never updated when elements leave the window. The third bug is at the end of the fixed window where all output elements up to `minp` are initialized to 0 if the input is not float. This PR fixes all three of the aforementioned bugs, at the cost of casting the output array to floating point even if the input is integer. This is less than ideal if the output has no NaNs, but is still consistent with roll_sum behavior.
…21704) User reported that `df.rolling(to_offset('3D'), closed='left').max()` segfaults when df has a datetime index. The bug was in PR pandas-dev#19549. In that PR, in https://github.com/pandas-dev/pandas/blame/master/pandas/_libs/window.pyx#L1268 `i` is initialized to `endi[0]`, which is 0 when `closed=left`. So in the next line when it tries to set `output[i-1]` it goes out of bounds. In addition, there are 2 more bugs in the `roll_min_max` code. The second bug is that for variable size windows, the `nobs` is never updated when elements leave the window. The third bug is at the end of the fixed window where all output elements up to `minp` are initialized to 0 if the input is not float. This PR fixes all three of the aforementioned bugs, at the cost of casting the output array to floating point even if the input is integer. This is less than ideal if the output has no NaNs, but is still consistent with roll_sum behavior.
…21704) User reported that `df.rolling(to_offset('3D'), closed='left').max()` segfaults when df has a datetime index. The bug was in PR pandas-dev#19549. In that PR, in https://github.com/pandas-dev/pandas/blame/master/pandas/_libs/window.pyx#L1268 `i` is initialized to `endi[0]`, which is 0 when `closed=left`. So in the next line when it tries to set `output[i-1]` it goes out of bounds. In addition, there are 2 more bugs in the `roll_min_max` code. The second bug is that for variable size windows, the `nobs` is never updated when elements leave the window. The third bug is at the end of the fixed window where all output elements up to `minp` are initialized to 0 if the input is not float. This PR fixes these three bugs, at the cost of casting the output array to floating point even if the input is integer. This is less than ideal if the output has no NaNs, but is consistent with roll_sum behavior.
…21704) User reported that `df.rolling(to_offset('3D'), closed='left').max()` segfaults when df has a datetime index. The bug was in PR pandas-dev#19549. In that PR, in https://github.com/pandas-dev/pandas/blame/master/pandas/_libs/window.pyx#L1268 `i` is initialized to `endi[0]`, which is 0 when `closed=left`. So in the next line when it tries to set `output[i-1]` it goes out of bounds. In addition, there are 2 more bugs in the `roll_min_max` code. The second bug is that for variable size windows, the `nobs` is never updated when elements leave the window. The third bug is at the end of the fixed window where all output elements up to `minp` are initialized to 0 if the input is not float. This PR fixes these three bugs, at the cost of casting the output array to floating point even if the input is integer. This is less than ideal if the output has no NaNs, but is consistent with roll_sum behavior.
git diff upstream/master -u -- "*.py" | flake8 --diff
In my testing the performance of
Went from 1.8 sec to 0.3 sec on my machine (lenovo laptop).