Quick sort: Replace recursion with custom stack, small improvements#84
Open
goldsteinn wants to merge 1 commit intoswenson:mainfrom
Open
Quick sort: Replace recursion with custom stack, small improvements#84goldsteinn wants to merge 1 commit intoswenson:mainfrom
goldsteinn wants to merge 1 commit intoswenson:mainfrom
Conversation
Instead of recursing just roll a custom stack with low/high bounds
of the next region.
Also tune some of the logic a bit.
- Simpler (and faster) median + setup for partition
- Remove some unnecessary branches in hot control flow.
Results in roughly 10% perf improvement on the project benchmarks:
(See PR for full run data)
4027959.8 / 4258144.7 -> 0.9459
Quick_sort 100000 x86_64 249 4027959.8 ns/op
Quick_sort 100000 x86_64 235 4258144.7 ns/op
Running tests with random numbers: 902582.0 / 940650.0 -> 0.9595
sort.h quick_sort - ok, 902582.0 usec
sort.h quick_sort - ok, 940650.0 usec
Running tests with same number: 8986.0 / 9059.0 -> 0.9919
sort.h quick_sort - ok, 8986.0 usec
sort.h quick_sort - ok, 9059.0 usec
Running tests with sorted numbers: 148790.0 / 160015.0 -> 0.9299
sort.h quick_sort - ok, 148790.0 usec
sort.h quick_sort - ok, 160015.0 usec
Running tests with sorted blocks of length 10: 872430.0 / 915431.0 -> 0.953
sort.h quick_sort - ok, 872430.0 usec
sort.h quick_sort - ok, 915431.0 usec
Running tests with sorted blocks of length 100: 751763.0 / 791987.0 -> 0.9492
sort.h quick_sort - ok, 751763.0 usec
sort.h quick_sort - ok, 791987.0 usec
Running tests with sorted blocks of length 10000: 461118.0 / 514853.0 -> 0.8956
sort.h quick_sort - ok, 461118.0 usec
sort.h quick_sort - ok, 514853.0 usec
Running tests with swapped size/2 pairs: 812161.0 / 854230.0 -> 0.9508
sort.h quick_sort - ok, 812161.0 usec
sort.h quick_sort - ok, 854230.0 usec
Running tests with swapped size/8 pairs: 522638.0 / 575848.0 -> 0.9076
sort.h quick_sort - ok, 522638.0 usec
sort.h quick_sort - ok, 575848.0 usec
Running tests with known evil data: 146601.0 / 196450.0 -> 0.7463
sort.h quick_sort - ok, 146601.0 usec
sort.h quick_sort - ok, 196450.0 usec
So roughly a 5-10% for most cases with the outliers being no-change
for same-number and 25% improvement for "evil data".
Author
|
Results from running |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Instead of recursing just roll a custom stack with low/high bounds of the next region.
Also tune some of the logic a bit.
- Simpler (and faster) median + setup for partition
- Remove some unnecessary branches in hot control flow.
Results in roughly 10% perf improvement on the project benchmarks: (See PR for full run data)
4027959.8 / 4258144.7 -> 0.9459
Quick_sort 100000 x86_64 249 4027959.8 ns/op
Quick_sort 100000 x86_64 235 4258144.7 ns/op
Running tests with random numbers: 902582.0 / 940650.0 -> 0.9595
sort.h quick_sort - ok, 902582.0 usec
sort.h quick_sort - ok, 940650.0 usec
Running tests with same number: 8986.0 / 9059.0 -> 0.9919
sort.h quick_sort - ok, 8986.0 usec
sort.h quick_sort - ok, 9059.0 usec
Running tests with sorted numbers: 148790.0 / 160015.0 -> 0.9299
sort.h quick_sort - ok, 148790.0 usec
sort.h quick_sort - ok, 160015.0 usec
Running tests with sorted blocks of length 10: 872430.0 / 915431.0 -> 0.953
sort.h quick_sort - ok, 872430.0 usec
sort.h quick_sort - ok, 915431.0 usec
Running tests with sorted blocks of length 100: 751763.0 / 791987.0 -> 0.9492
sort.h quick_sort - ok, 751763.0 usec
sort.h quick_sort - ok, 791987.0 usec
Running tests with sorted blocks of length 10000: 461118.0 / 514853.0 -> 0.8956
sort.h quick_sort - ok, 461118.0 usec
sort.h quick_sort - ok, 514853.0 usec
Running tests with swapped size/2 pairs: 812161.0 / 854230.0 -> 0.9508
sort.h quick_sort - ok, 812161.0 usec
sort.h quick_sort - ok, 854230.0 usec
Running tests with swapped size/8 pairs: 522638.0 / 575848.0 -> 0.9076
sort.h quick_sort - ok, 522638.0 usec
sort.h quick_sort - ok, 575848.0 usec
Running tests with known evil data: 146601.0 / 196450.0 -> 0.7463
sort.h quick_sort - ok, 146601.0 usec
sort.h quick_sort - ok, 196450.0 usec
So roughly a 5-10% for most cases with the outliers being no-change for same-number and 25% improvement for "evil data".