BOOL vs bool speed

Started by
12 comments, last by Warpstorm 19 years, 11 months ago
I am curious which is actually faster in MS C++. Don''t divert this into a discussion about which I should use style wise. I searched this site for answers to this question which has been asked before and nobody actually answered the question. They just diverted it into dogma about how bool is better because it is the standard.
Advertisement
Faster? Hmm, I really doubt you''re going to get a noticeable speed difference using one or the other. I''m assuming BOOL is just a define to char ... or maybe to int, who knows. Either way, won''t it get blown up to an integer anyway, regardlessif it''s a char or an int in the first place?
Well anyway, don''t get hung up on trying to optimize this kind of crap IMO. Concentrate on writing efficient algorithms instead.
Yeah, basically what Red Ant said. BOOL is a typedef for int, thus, on a normal platform with MS VC (version 6, I''m assuming, I don''t know about the others), a BOOL is 4 times as large as a bool. If you have a large array of these values, then it would probably be best to use bool, to save space. In any other situation, though, there''s no reason, really. Microsoft made BOOL an int instead of a char or bool so that it would be identical to the word size of the standard processor (32 bits). This may add a small small bit of performance to programs in certain areas. Usually, none of this is worth worrying over.

Of course, in the one situation mentioned above, where you have a really large array, and you really need to save space, then it would probably be best to use the bitwise operators to actually get and set each individual bit. Compared to using BOOL, you would only use 1/32 of the total space BOOL would use. But it''ll take a bit more processing to get/set the values. Usually nothing significant, though, especially when space is more valuable than time.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
I would guess that would be platform specific and you will never know for sure and thats why there most probably isn''t a definite answer.

BOOL is just a type alias of an integer and on 32 bit architectures will be 4 bytes big on high/lower bit archs it will be something else, its not portable if you wonted to use it non MS OSs you''ll need to typedef it yourself. I guess it was created because the Win API is a C lib and C doesn''t have built-in boolean type, unless one has been add in C99 now.

bool is a built-in primitive type of C++, its 1 bit long, because it''s a built-in type it will work on all C++ compilers and is more type safe.

I don''t much about IA-32 type architectures but i do know that Microcontrollers support built-in bit operations and they take one cycle to set/clear a bit but if you used a whole register as your boolean it may take 1 or 2 cycles depending on the operation and its operands.

I dont think this is a matter of style, if your using a language you use it''s features, if your programming in C++ choosing BOOL over built-in bool and using it as it''s intended to be used is silly.
quote:Original post by Warpstorm
I am curious which is actually faster in MS C++.

Don''t divert this into a discussion about which I should use style wise. I searched this site for answers to this question which has been asked before and nobody actually answered the question. They just diverted it into dogma about how bool is better because it is the standard.

Well, rather than diverting it into a style discussion (where bool obviously wins) I''m going to divert it into a premature optimization discussion.
It really, really, really doesn''t matter which is faster. The speed difference is not going to be noticeable. Use bool. When you''re closer to completion, profile your code. If by some miracle your code is so well written that using bool is actually in the list of the top 20 things that makes your code slow, then change the type. If you can''t tell whether using bool is slowing you down, then use a typedef instead (so you can easily change all occurances) and test both types.

There''s another reason that I can''t give you a straight answer: It depends on how the variables are being used. For example, in some cases, using BOOL may push you over a size boundary, and slow you down, whereas in other cases, using BOOL may push your data just up to a size boundary and make the code faster. Yet another reason is that the speed differences will depend on the machine architecture.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
quote:Original post by Red Ant
Well anyway, don''t get hung up on trying to optimize this kind of crap IMO. Concentrate on writing efficient algorithms instead.


This isn''t really about optimization, but rather to settle an argument. What is faster using bools or BOOLs on a Microsoft Windows environment? Cross platform is of absoulute no concern to us.

Well, can''t you just run 1 million bool comparisons using both types and take the time?
quote:Original post by snk_kid
bool is a built-in primitive type of C++, its 1 bit long, because it''s a built-in type it will work on all C++ compilers and is more type safe.

The size of a boolean is, like most data type sizes in C++, not exactly defined. You are guaranteed that 1<=sizeof(bool)<=sizeof(long) (Stroustrup, The C++ Programming Language, 3rd edition, 4.6); since there is no upper bound on a long, and since you are guaranteed that a char holds at least 8 bits (and sizeof(char) is 1 by definition), a bool is at least 8 bits. On my platform it appears to be exactly 8 bits; on yours, it may or may not.

It is true that a std::vector<bool> is a bit vector, with all the good and the bad that it implies ...
It''s also the case that compiler options may change the size of bool, correct?

If you are trying to settle a debate, why not definitively test it as someone else suggested. Why bring it up in these forums where you can never get a straight answer and in some cases you will get simply wrong information (like a bool only takes up 1-bit)

Regards,
Jeff



[ CodeDread ]
quote:Original post by Anonymous Poster
This isn''t really about optimization, but rather to settle an argument. What is faster using bools or BOOLs on a Microsoft Windows environment? Cross platform is of absoulute no concern to us.
It pretty much completely depends upon A) what processor you''re using, and more importantly B) how you''re using booleans in your code. A BOOL might be faster on an average processor for algorithm 1, while for algorithm 2, a bool will be faster, using the same average processors. Heck, on a few processors, it might switch on a few algorithms. In the end, it is really hard to say which is faster. The most definite situation I can think of, as I said earlier, is when you have a huge amount of booleans. In that case, it is better to reduce space, partially just for the sake of reducing space, but also to help with keeping everything in the cache, not having to use virtual memory, etc. Those sorta things will affect speed noticeably, but only if you have a large enough number of booleans for it to matter. Otherwise, it''s pretty hard (and pointless) to say which is faster.

Most of the greatest evils that man has inflicted upon man have come through people feeling quite certain about something which, in fact, was false.

-Bertrand Russell, Unpopular Essays, "Ideas That Have Harmed Mankind"
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement