 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ted Carter Guest
|
Posted: Wed Dec 06, 2006 10:10 am Post subject: bool and optimization |
|
|
I'm deciding whether to use a bitvector versus a bool type in a class.
// Example 1
class A1
{
bool on_;
bool dummy_;
public:
A1() : on_(false), dummy_(false) {}
bool is_on() const { return on_; }
// ...
};
// Example 2
template <uint8 position>
class A2
{
static uint32 bitvector_;
bool dummy_;
public:
A2() : dummy_(false) {}
bool is_on() const { return bitvector_ & (1 << position); }
// ...
};
bool A2::bitvector_ = 0;
Will a half decent C++ compiler optimize example 1 to be equivalent to
example 2 in both performance and code size, or do I need to take care of
business myself?
Regards, Ted.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Dec 06, 2006 10:10 am Post subject: Re: bool and optimization |
|
|
| Quote: | Will a half decent C++ compiler optimize example 1 to be equivalent to
example 2 in both performance and code size, or do I need to take care of
business myself?
The classes are very different, so no it won't. A2 is a class template, |
which means that we have a number of template classes generated from
it. There will be one uint32 for EVERY class generated, which means
that only one bit of it is used. A2 will only work if a single object
at a time is using the same position, also we are limited to 32
(assuming uint32 is 32-bit unsigned integer) simultanous A2<N> objects.
Now if you wonder whether it can prove that no more than 32 simultanous
objects exist at a time, then I doubt many optimizers will optimize it.
If what you ask is whether it will only use 1 bit of storage, then it
might, or it might not. It will require a little extra processing, but
save a little memory. In most cases the little memory gain is
insignificant and optimizers ignore the possibility. std::vector<bool>
is an interresting case of such an optimization, it explictely
specifies that it does use a proxy object to get 1-bit boolean objects.
However, the best way to figure it out is try to compile the code on a
couple of "half decent C++ compilers" and check the assembly output
(GCC and VC should probably be tested).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Thu Dec 07, 2006 1:34 am Post subject: Re: bool and optimization |
|
|
Ted Carter wrote:
| Quote: | I'm deciding whether to use a bitvector versus a bool type in a class.
// Example 1
class A1
{
bool on_;
bool dummy_;
public:
A1() : on_(false), dummy_(false) {}
bool is_on() const { return on_; }
// ...
};
// Example 2
template <uint8 position
class A2
{
static uint32 bitvector_;
bool dummy_;
public:
A2() : dummy_(false) {}
bool is_on() const { return bitvector_ & (1 << position); }
// ...
};
bool A2::bitvector_ = 0;
Will a half decent C++ compiler optimize example 1 to be equivalent to
example 2 in both performance and code size,
|
Not clear in which way equivalent.
| Quote: | or do I need to take care of business myself?
|
If memory usage is an issue, bit-fields might be a good idea.
struct some
{
uint32_t is_foo : 1;
uint32_t is_bar : 1;
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|