Optimize is_permutation for vector<bool>#6148
Open
AlexGuteniev wants to merge 9 commits intomicrosoft:mainfrom
Open
Optimize is_permutation for vector<bool>#6148AlexGuteniev wants to merge 9 commits intomicrosoft:mainfrom
is_permutation for vector<bool>#6148AlexGuteniev wants to merge 9 commits intomicrosoft:mainfrom
Conversation
Member
|
I believe this could be generalized further. For any ranges where the value types are |
Contributor
Author
|
Generalized, benchmark results are about the same |
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.
I was looking into bit tricks for
next_permutation/prev_permutation.Seems like that there will be no impressive results, as these operations perform already at ~15 ns order of magnitude.
I may further look into these though.
But let's go for the easiest optimization here that gains 100x and more!
The optimized algorithm does mismatch, and then counts
truevalues in each of the remaining ranges.Mismatch
The
mismatchpart serves to save the equality case optimization that is implied by the standard, asking for N**2 comparisons generally, but just N if the ranges are equal. If none of the ranges isvector<bool>then we have these cases:mismatchturns to vector mismatch, andcountto vector countmismatchis slightly fastermismatchcall that will only likely misalign the input for thecountcall due to advancing few elementsmismatchthe number of comparison is twice smaller for equal cases.counts is vectorized, somismatchis not that much fasterSo overall
mismatchshould be there.One
vector<bool>or both of them flip it:mismatchforvector<bool>is not optimized currentlycountis optimized equally well for aligned and misaligned casesBenchmark results
Interim version is without
mismatch. Its timings are not used is speedup calculation.Array:
vector<bool>, Interim column is irrelevant, it does not show any data different from After.