Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:21:04+00:00 2026-06-05T03:21:04+00:00

I was just toying with search algorithms a while ago and after a few

  • 0

I was just toying with search algorithms a while ago and after a few benchmarks, I was impressed to see how much faster the old bsearch() was comparend to std::binary_search(). I thought that any decent compiler would be able to replace std::binary_search() by bsearch() when possible, but even though I’m using GCC 4.7, bsearch seems to perform something like 5 times faster than std::binary_search.

So I thought that it would be a great exercise trying to create some kind of wrapper for bsearch with the same interface then std::binary_search. But for an unknown reason, I did not manage to do it. Here’s my code:

template<typename InputIterator, class T>
bool binary_search(InputIterator first, InputIterator last, const T& value)
{
    auto cmp = [](const void* a, const void* b)
    {
        return (int) ((*(T*)a) == (*(T*)b));
    };

    std::cout << value << std::endl;
    T* res = (T*) bsearch(&value, first, last-first, sizeof(*first), cmp);
    return res != nullptr;
}

The code compiles fine and does not crash at execution. However, it seems that bsearch stops right after one internal iteration (*res is always equals to the value at the middle of the tab passed as parameter). I can’t manage to find why it does not work. So, if possible, a little help would be fine.

Thanks.


For those who ask for the code used to check the speed:

const std::string keyword_str[] = {
    // Some strings
};

int cmp(const void* s1, const void* s2)
{
    return (int) ((*(std::string*)s1) == (*(std::string*)s2));
}

int main()
{
    time_t start, end;
    double dif;
    time (&start);

    // Code
    for (const string& str: keyword_str)
    {
        for (size_t i = 0 ; i < 1000000 ; ++i)
        {
            // std::binary_search (uncomment to check)
            //bool a = std::binary_search(keyword_str, keyword_str+28, str);

            // bsearch
            char** st = (char**) bsearch(&str, keyword_str, 28, sizeof(keyword_str[0]), cmp);
        }
    }

    time (&end);
    dif = difftime (end, start);
    printf("Time spent: %fs.\n", dif);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:21:05+00:00Added an answer on June 5, 2026 at 3:21 am

    bsearch takes a function pointer, and cmp is not a function pointer. (EDIT: I was wrong about this. Since cmp doesn’t capture any variables — it’s brackets are empty — it can be passed as a function pointer. This behavior is specified in §5.1.2/6 of the C++11 standard.)

    bsearch also doesn’t return the right values that the comparison function is expected to return. It should return -1 if the key is less than the array element, 0 if they are equal, and 1 if the key is greater than the array element. Your cmp function returns 0 if they are inequal and 1 if they are equal. As a result, if the first element you’re comparing is inequal to the key, then your cmp makes bsearch think they’re equal and bsearch stops because it thinks it found the right element immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, Stupid question here... just trying to do a search in an array to
Just toying with the SDK and I was wondering if possible a UITouch event
I have the following in my .php file. Just trying a simple search on
I'm just trying out full text search in SQL Server 2008 and am failing
Just trying to get diff to work better for certain kinds of documents. With
just trying to test for equality in this piece of code, but getting a
Just trying to still get my head around IOC principles. Q1: Static Methods -
Just trying to establish whether prototype can do something like $$('#ID a:last').css('color','#111'); Any ideas
Just trying to get my head around what can happen when things go wrong
Just trying to canvas some opinions here. I was wondering how people go about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.