diff options
author | André Goddard Rosa <andre.goddard@gmail.com> | 2010-03-28 21:18:56 -0300 |
---|---|---|
committer | André Goddard Rosa <andre.goddard@gmail.com> | 2010-03-28 21:18:56 -0300 |
commit | acdb052030d6e537e51d8b42e07b2a9e71e6ee42 (patch) | |
tree | c42e974950bbe7d89eac29f62381d8d7e759f69b /libc | |
parent | 4ad72f89b12fe0157074a253190f6aa2cbfe15ac (diff) | |
download | bionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.zip bionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.tar.gz bionic-acdb052030d6e537e51d8b42e07b2a9e71e6ee42.tar.bz2 |
stdlib: optimize bsearch()
... by checking most probable condition first (elements do differ)
Change-Id: I424eab9c32a6d9eb82b686ca04025ec8c9097035
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Diffstat (limited to 'libc')
-rw-r--r-- | libc/stdlib/bsearch.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/libc/stdlib/bsearch.c b/libc/stdlib/bsearch.c index 8193d27..7eb6325 100644 --- a/libc/stdlib/bsearch.c +++ b/libc/stdlib/bsearch.c @@ -56,11 +56,11 @@ bsearch(const void *key, const void *base0, size_t nmemb, size_t size, for (lim = nmemb; lim != 0; lim >>= 1) { p = base + (lim >> 1) * size; cmp = (*compar)(key, p); - if (cmp == 0) - return ((void *)p); if (cmp > 0) { /* key > p: move right */ base = (char *)p + size; lim--; + } else if (cmp == 0) { + return ((void *)p); } /* else move left */ } return (NULL); |