-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Merged
jreback
merged 22 commits into
pandas-dev:master
from
hexgnu:performance_increase_rolling_min_max
Feb 14, 2018
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
737c033
First stab at using deque over full iterations
hexgnu 15d2563
Working deque implementation of min/max
hexgnu 06f2658
oops
hexgnu 8089e67
Remove some extraneous variables
hexgnu 08ff553
Get rid of some of the branches in the code
hexgnu 6ef87b2
Revert "Get rid of some of the branches in the code"
hexgnu 6e8c041
Prefer cmath over math.h for cpp
hexgnu b0a0ef6
Change to std namespace
hexgnu b19774e
Fix issue with variable window size
hexgnu 92857ee
Oh right variable window size ;)
hexgnu 832ff9d
Use std namespace for windows compilation
hexgnu f00e994
Add cmath so build will complete
hexgnu 0d713be
Merge remote-tracking branch 'upstream/master' into performance_incre…
hexgnu 1ab4e21
Fix linting error in window.pyx
hexgnu d5b60cd
I think this will fix MSVC build
hexgnu 42f8fdf
I think this is what I want to do
hexgnu 38e3f70
Add documentation
hexgnu 7f4abf9
Add another benchmark to test variable window methods
hexgnu 23fe816
Ugh use // not #
hexgnu 060dfb7
New better benchmark
hexgnu aeb9b9b
Add whatsnew entry
hexgnu 65c0dbe
Merge remote-tracking branch 'upstream/master' into performance_incre…
hexgnu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef _PANDAS_MATH_H_ | ||
#define _PANDAS_MATH_H_ | ||
|
||
// In older versions of Visual Studio there wasn't a std::signbit defined | ||
// This defines it using _copysign | ||
#if defined(_MSC_VER) && (_MSC_VER < 1800) | ||
#include <cmath> | ||
namespace std { | ||
__inline int signbit(double num) { return _copysign(1.0, num) < 0; } | ||
} | ||
#else | ||
#include <cmath> | ||
#endif | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
cimport cython | ||
from cython cimport Py_ssize_t | ||
from libcpp.deque cimport deque | ||
|
||
from libc.stdlib cimport malloc, free | ||
|
||
|
@@ -12,7 +13,7 @@ from numpy cimport ndarray, double_t, int64_t, float64_t | |
cnp.import_array() | ||
|
||
|
||
cdef extern from "../src/headers/math.h": | ||
cdef extern from "../src/headers/cmath" namespace "std": | ||
int signbit(double) nogil | ||
double sqrt(double x) nogil | ||
|
||
|
@@ -1222,8 +1223,9 @@ cdef _roll_min_max(ndarray[numeric] input, int64_t win, int64_t minp, | |
cdef: | ||
numeric ai | ||
bint is_variable, should_replace | ||
int64_t s, e, N, i, j, removed | ||
int64_t N, i, removed, window_i | ||
Py_ssize_t nobs = 0 | ||
deque Q[int64_t] | ||
ndarray[int64_t] starti, endi | ||
ndarray[numeric, ndim=1] output | ||
cdef: | ||
|
@@ -1242,32 +1244,48 @@ 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 commentThe 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) |
||
Q = deque[int64_t]() | ||
|
||
if is_variable: | ||
|
||
with nogil: | ||
|
||
for i in range(N): | ||
s = starti[i] | ||
e = endi[i] | ||
# This is using a modified version of the C++ code in this | ||
# SO post: http://bit.ly/2nOoHlY | ||
# The original impl didn't deal with variable window sizes | ||
# So the code was optimized for that | ||
|
||
r = input[s] | ||
nobs = 0 | ||
for j in range(s, e): | ||
for i from starti[0] <= i < endi[0]: | ||
ai = init_mm(input[i], &nobs, is_max) | ||
|
||
# adds, death at the i offset | ||
ai = init_mm(input[j], &nobs, is_max) | ||
if is_max: | ||
while not Q.empty() and ai >= input[Q.back()]: | ||
Q.pop_back() | ||
else: | ||
while not Q.empty() and ai <= input[Q.back()]: | ||
Q.pop_back() | ||
Q.push_back(i) | ||
|
||
if is_max: | ||
if ai > r: | ||
r = ai | ||
else: | ||
if ai < r: | ||
r = ai | ||
for i from endi[0] <= i < N: | ||
output[i-1] = calc_mm(minp, nobs, input[Q.front()]) | ||
|
||
output[i] = calc_mm(minp, nobs, r) | ||
ai = init_mm(input[i], &nobs, is_max) | ||
|
||
else: | ||
if is_max: | ||
while not Q.empty() and ai >= input[Q.back()]: | ||
Q.pop_back() | ||
else: | ||
while not Q.empty() and ai <= input[Q.back()]: | ||
Q.pop_back() | ||
|
||
while not Q.empty() and Q.front() <= i - (endi[i] - starti[i]): | ||
Q.pop_front() | ||
|
||
Q.push_back(i) | ||
|
||
output[N-1] = calc_mm(minp, nobs, input[Q.front()]) | ||
|
||
else: | ||
# setup the rings of death! | ||
ring = <numeric *>malloc(win * sizeof(numeric)) | ||
death = <int64_t *>malloc(win * sizeof(int64_t)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :>)