Optimization attempt of findPeriod using binary search#163
Open
rveilleux wants to merge 1 commit intoAmigaPorts:mainfrom
Open
Optimization attempt of findPeriod using binary search#163rveilleux wants to merge 1 commit intoAmigaPorts:mainfrom
rveilleux wants to merge 1 commit intoAmigaPorts:mainfrom
Conversation
…d table O(log N) instead of O(N) lookups
Contributor
Author
|
Note: Even while it performs less lookup in average, the loop is more complex, so perhaps real-world gain will be small. |
Member
|
It's a funny thing - I've done something similar in the past, emailed Frank W. about it and he told me he's unsure that the optimization is worth it. I've checked my code again by running it in release mode (-O3 compiler option) and it was horribly slower than his trivial array traversal. Have you done some measurements with Bartman's profiler and/or setting custom.color[0] to fixed color during the function run? Lemme know how's the performance before/after. My code looked like this: static inline UBYTE findPeriod(UWORD *pPeriods, UWORD uwNote) {
// Find nearest period for a note value
// https://stackoverflow.com/questions/6553970/
UBYTE ubLow = 0, ubHigh = MOD_PERIOD_TABLE_LENGTH;
while (ubLow != ubHigh) {
UBYTE ubMid = (ubLow + ubHigh) / 2;
if (uwNote < pPeriods[ubMid]) {
// This index, and everything below it, is not the first element
// greater/equal than what we're looking for because
// this element is no greater/equal than the element.
ubLow = ubMid + 1;
}
else {
// This element is at least as large as the element, so anything after it can't
// be the first element that's at least as large.
ubHigh = ubMid;
}
}
} |
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.
Optimized findPeriod to use a binary search into the 36 elements period table
O(log N) instead of O(N) lookups