O
Ole Nielsby
My app (an interpreter for a homebrew language) does lots of
binary searches on sorted arrays of unique keys, for looking up
variables and methods.
Till now I used std::equal_range() for the searching.
I just replaced it with a homebrew binary search which
gave about 3% overall performance improvement of
my interpreter (VC9 release build, using a "real" app
as benchmark).
I figure it's because std::equal_range needs to widen the
range in both directions when a hit is found.
But it feels like I've reinventing the wheel. So I wonder if
there is a standard function I have overlooked?
Stl has a binary_search() algorithm that tests for existence
but it doesn't return the position so it's not of any use.
There is also a standard C funciton bsearch() but it uses a
comparison function ptr, which is overkill for the fast and
simple pointer comparisons I use.
BTW my program compiles with VC9 and GCC but when
I tried compiling parts of it for Symbian I got errors for cases
like this:
===========code==============
#include <algorithm>
class A{};
class B : public A {public: B(){}};
int main(){
const B b;
const A *const aa[1] = {&b};
std:
air<const A*const*, const A*const*> range
= std::equal_range(&aa[0], &aa[1], &b);
}
==========/code===============
They are easily fixed by supplying template arguments:
std::equal_range<const A* const*, const A*>(&aa[0], &aa[1], &b);
I'm just curious to know if it's the Symbion compiler/stlport that is
flawed or GCC/VC happen to be permissive?
binary searches on sorted arrays of unique keys, for looking up
variables and methods.
Till now I used std::equal_range() for the searching.
I just replaced it with a homebrew binary search which
gave about 3% overall performance improvement of
my interpreter (VC9 release build, using a "real" app
as benchmark).
I figure it's because std::equal_range needs to widen the
range in both directions when a hit is found.
But it feels like I've reinventing the wheel. So I wonder if
there is a standard function I have overlooked?
Stl has a binary_search() algorithm that tests for existence
but it doesn't return the position so it's not of any use.
There is also a standard C funciton bsearch() but it uses a
comparison function ptr, which is overkill for the fast and
simple pointer comparisons I use.
BTW my program compiles with VC9 and GCC but when
I tried compiling parts of it for Symbian I got errors for cases
like this:
===========code==============
#include <algorithm>
class A{};
class B : public A {public: B(){}};
int main(){
const B b;
const A *const aa[1] = {&b};
std:
= std::equal_range(&aa[0], &aa[1], &b);
}
==========/code===============
They are easily fixed by supplying template arguments:
std::equal_range<const A* const*, const A*>(&aa[0], &aa[1], &b);
I'm just curious to know if it's the Symbion compiler/stlport that is
flawed or GCC/VC happen to be permissive?