473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<bool> specialisation performance issue

On my platform I find that the std::vector<boolspecialisation incurs a
significant performance hit in some circumstances (when compared, say, to
std::vector<intprogrammed analagously). Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?

Say I do:

typedef bool boolreally;
std::vector<booleallybvec;

do I still get the std::vector<boolspecialisation? (I suspect the answer
is "yes", but I'm not sure how typedef-ed types are interpreted as
template parameters).

I can (and currently do) use std::vector<intat times, but I'd rather
not, when what I want really is "bool" rather than "int" (or anything else).

As I understand it, the justification for the std::vector<bool>
specialisation is to reduce the space overhead (possibly at the cost of
a time overhead). Personally I can't envisage many circumstances where one
mightn't use std::bitset rather than std::vector<boolif space were
an issue... I thus find the specialisation somewhat vexing. Am I
misunderstanding something here?

--
Lionel B
Dec 8 '06 #1
8 5354

Lionel B napsal:
On my platform I find that the std::vector<boolspecialisation incurs a
significant performance hit in some circumstances (when compared, say, to
std::vector<intprogrammed analagously). Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?

Say I do:

typedef bool boolreally;
std::vector<booleallybvec;

do I still get the std::vector<boolspecialisation? (I suspect the answer
is "yes", but I'm not sure how typedef-ed types are interpreted as
template parameters).
typedef'ed types are equivalent for template instantiation. You can
test it with following code:

#include <iostream>

template<typename T>
void Test()
{
std::cout << "Test<T>\n";
}

template<>
void Test<int>()
{
std::cout << "Test<int>\n";
}

int main(int argc, char* argv[])
{
typedef
int INT;

Test<INT>();
}
I can (and currently do) use std::vector<intat times, but I'd rather
not, when what I want really is "bool" rather than "int" (or anything else).

As I understand it, the justification for the std::vector<bool>
specialisation is to reduce the space overhead (possibly at the cost of
a time overhead). Personally I can't envisage many circumstances where one
mightn't use std::bitset rather than std::vector<boolif space were
an issue... I thus find the specialisation somewhat vexing. Am I
misunderstanding something here?
std::bitset has fixed size, std::vector<boolnot.

Dec 8 '06 #2
On Fri, 08 Dec 2006 05:59:14 -0800, Ondra Holub wrote:
Lionel B napsal:
>On my platform I find that the std::vector<boolspecialisation incurs a
significant performance hit in some circumstances (when compared, say, to
std::vector<intprogrammed analagously). Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?

Say I do:

typedef bool boolreally;
std::vector<booleallybvec;

do I still get the std::vector<boolspecialisation? (I suspect the answer
is "yes", but I'm not sure how typedef-ed types are interpreted as
template parameters).
typedef'ed types are equivalent for template instantiation. You can
test it with following code:

[snip]
Yup, thanks.
[snip]
>>
As I understand it, the justification for the std::vector<bool>
specialisation is to reduce the space overhead (possibly at the cost of
a time overhead). Personally I can't envisage many circumstances where one
mightn't use std::bitset rather than std::vector<boolif space were
an issue... I thus find the specialisation somewhat vexing. Am I
misunderstanding something here?

std::bitset has fixed size,
Oops, so it has.
std::vector<boolnot.
As it happens, I have just read an article suggesting that one way to spoof
std::vector<boolis to provide an explicit allocator, in which case the
non-specialised std::vector<boolwill be used... maybe overkill in my
case.

--
Lionel B
Dec 8 '06 #3

Lionel B wrote:
On my platform I find that the std::vector<boolspecialisation incurs a
significant performance hit in some circumstances (when compared, say, to
std::vector<intprogrammed analagously). Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?
struct Bool {
Bool(bool b) : b(b) { }
bool b;
operator bool() { return b; }
// etc...
};

Also try std::deque<bool>. It does hold bools.

HTH,
Michiel Salters

Dec 8 '06 #4
On Fri, 8 Dec 2006 13:24:16 +0000 (UTC) in comp.lang.c++, "Lionel B"
<me@privacy.netwrote,
>Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?
Good question. I would be inclined to try a vector<unsigned charand
let default conversions take care of the rest.

Dec 8 '06 #5
On Fri, 08 Dec 2006 17:33:40 +0000, David Harmon wrote:
On Fri, 8 Dec 2006 13:24:16 +0000 (UTC) in comp.lang.c++, "Lionel B"
<me@privacy.netwrote,
>>Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?

Good question. I would be inclined to try a vector<unsigned charand
let default conversions take care of the rest.
I have done something like that before but it makes me - probably
unjustifiably - nervous... I can never somehow convince myself that
the default conversions will always do the "right thing"; i.e. at
the very least behave exactly like bool in all comparisons and assignments
with, to and from real bools as well as with, to and from the spoofed
bool (never mind with, to and from all other integer types...). Can this be
guaranteed?

--
Lionel B
Dec 8 '06 #6

Lionel B wrote:
On Fri, 08 Dec 2006 17:33:40 +0000, David Harmon wrote:
On Fri, 8 Dec 2006 13:24:16 +0000 (UTC) in comp.lang.c++, "Lionel B"
<me@privacy.netwrote,
>Is it possible to "spoof"
std::vector into implementing a "true" vector of bool rather than the
specialisation?
Good question. I would be inclined to try a vector<unsigned charand
let default conversions take care of the rest.

I have done something like that before but it makes me - probably
unjustifiably - nervous... I can never somehow convince myself that
the default conversions will always do the "right thing"; i.e. at
the very least behave exactly like bool in all comparisons and assignments
with, to and from real bools as well as with, to and from the spoofed
bool (never mind with, to and from all other integer types...). Can this be
guaranteed?
It probably doesnt help your code (if it needs to interact with old
code), but IMO bool would be better by far if it didnt have automatic
conversions from a large range of types. In fact it would be
interesting to try a bool UDT which didnt allow the conversions, except
by comparisons and so on. It may actually run faster than the original
code due to a reduction in the number of conversions and also may
clarify the code which again is good for speed. It would also solve
your problem ;-)

regards
Andy Little

Dec 8 '06 #7
"Lionel B" <me@privacy.netwrote in message
news:el**********@south.jnrs.ja.net...
As it happens, I have just read an article suggesting that one way to
spoof
std::vector<boolis to provide an explicit allocator, in which case the
non-specialised std::vector<boolwill be used... maybe overkill in my
case.
Bad article. vector<boolis partially specialized on its allocator type.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Dec 8 '06 #8

Lionel B wrote in message ...
>On Fri, 08 Dec 2006 05:59:14 -0800, Ondra Holub wrote:
>[snip]
std::bitset has fixed size,

Oops, so it has.
>std::vector<boolnot.

As it happens, I have just read an article suggesting that one way to spoof
std::vector<boolis to provide an explicit allocator, in which case the
non-specialised std::vector<boolwill be used... maybe overkill in my
case.
Well, if you are willing to 'overkill' anyway:

bool bl(true);
std::vector< std::bitset<1 VecBit( 7, 1 );
if( bl == VecBit.at(0)[0] ){
std::cout<<"VecBit.at(0)="<<VecBit.at(0)<<std::end l;
std::cout<<" ="<<std::boolalpha<<VecBit.at(0)[0]<<std::endl;
}
// out: VecBit.at(0)=1
// out: =true
<G>

[ seems I heard this was deprecated, I may be thinking of something else. ]
// Example (stl docs) // #include <vector>
std::bit_vector V(5);
V[0] = true;
V[1] = false;
V[2] = false;
V[3] = true;
V[4] = false;

for( std::bit_vector::iterator i(V.begin()); i < V.end(); ++i )
std::cout<<std::boolalpha<<(*i)<<std::endl;
/* - out -
true
false
false
true
false
*/

--
Bob R
POVrookie
Dec 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14356
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a program in which I need to store a series of Boolean values. A vector<bool> would be ideal. However, I'm concerned about this data structure because of Scott Meyers'...
3
4132
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
3
3447
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
11
1541
by: Michael | last post by:
Righty, 1: Is there a standard library that contain matrices and complex numbers. I need to find eigen values of a 3x3 matrix. 2: Is there a way of getting the pointer to the start of an...
1
2398
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
8
3528
by: Bo Peng | last post by:
Dear list, I am using std::vector<bool> (bit_vector) to store my bit sequence. To access the same sequence from C (to expose to a python module), I need to know the pointer and offset of...
12
6684
by: Piotr | last post by:
In effective STL, it said one should not use vector<bool> but use dequeue<bool> instead. But can dequeue<bool> has random access iterator? and I do this? dequeue<bool> myboolarray; if...
6
6784
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.