Created
August 11, 2023 17:21
Fast and slow (?) bsearch
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
static uint32_t * | |
fast_bsearch_u32(uint32_t *base, size_t nmemb, uint32_t key) { | |
while (nmemb) { | |
size_t half = nmemb >> 1; | |
if (base[half] < key) { | |
base += nmemb - half; | |
} | |
nmemb = half; | |
} | |
return base; | |
} | |
static uint32_t * | |
slow_bsearch_u32(uint32_t *base, size_t nmemb, uint32_t key) { | |
while (nmemb) { | |
size_t half = nmemb >> 1; | |
if (base[half] < key) { | |
base += half + 1; | |
nmemb -= half + 1; | |
} else { | |
nmemb = half; | |
} | |
} | |
return base; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment