I notice that vector is much slower than bool array when running the following code.
int main()
{
int count = 0;
int n = 1500000;
// slower with c++ vector<bool>
/*vector<bool> isPrime;
isPrime.reserve(n);
isPrime.assign(n, true);
*/
// faster with bool array
bool* isPrime = new bool[n];
for (int i = 0; i < n; ++i)
isPrime[i] = true;
for (int i = 2; i< n; ++i) {
if (isPrime[i])
count++;
for (int j =2; i*j < n; ++j )
isPrime[i*j] = false;
}
cout << count << endl;
return 0;
}
Is there some way that I can do to make vector<bool>
faster ? Btw, both std::vector::push_back
and std::vector::emplace_back
are even slower than std::vector::assign
.
vector<bool>
can be high performance, but isn't required to be. Forvector<bool>
to be efficient, it needs to operate on many bools at a time (e.g.isPrime.assign(n, true)
), and the implementor has had to put loving care into it. Indexing individual bools in avector<bool>
is slow.Here is a prime finder that I wrote a while back using
vector<bool>
and clang + libc++ (the libc++ part is important):This executes for me in about 28s (-O3). When I change it to return a
vector<char>
instead, the execution time goes up to about 44s.If you run this using some other std::lib, you probably won't see this trend. On libc++ algorithms such as
std::find
have been optimized to search a word of bits at a time, instead of bit at a time.See http://howardhinnant.github.io/onvectorbool.html for more details on what std algorithms could be optimized by your vendor.
vector<bool>
may have a template specialization and may be implemented using bit array to save space. Extracting and saving a bit and converting it from / tobool
may cause the performance drop you are observing. If you usestd::vector::push_back
, you are resizing the vector which will cause even worse performance. Next performance killer may beassign
(Worst complexity: Linear of first argument), instead useoperator []
(Complexity: constant).On the other hand,
bool []
is guaranteed to be array ofbool
.And you should resize to
n
instead ofn-1
to avoid undefined behaviour.std::vector<bool>
can have various performance issues (e.g. take a look at https://isocpp.org/blog/2012/11/on-vectorbool).In general you can:
use
std::vector<std::uint8_t>
instead ofstd::vector<bool>
(give a try tostd::valarray<bool>
also).This requires more memory and is less cache-friendly but there isn't a overhead (in the form of bit manipulation) to access a single value, so there are situations in which it works better (after all it's just like your array of
bool
but without the nuisance of memory management)std::bitset
if you know at compile time how large your boolean array is going to be (or if you can at least establish a reasonable upper bound)boost::dynamic_bitset
(the size can be specified at runtime)But for speed optimizations you have to test...
With your specific example I can confirm a performance difference only when optimizations are turned off (of course this isn't the way to go).
Some tests with g++ v4.8.3 and clang++ v3.4.5 on an Intel Xeon system (
-O3
optimization level) give a different picture:(time elapsed for 100 runs of the code in the answer)
std::vector<bool>
doesn't look so bad (source code here).