BOOL vs bool speed

Started by
12 comments, last by Warpstorm 19 years, 11 months ago
quote:Original post by Red Ant
Well, can''t you just run 1 million bool comparisons using both types and take the time?


No, this is completely pointless.
Read what JohnBSmall said. If you don''t understand his reply, then you shouldn''t even be thinking about these kinds of optimisations.

And if you need to worry about an array of bools taking up too much space, then maybe you need to rethink your overall method.
Advertisement
It think usage really depends on where your primary bottleneck hits at.

If you''re short on memory, the smaller data size of bool is probably a good option, but using BOOL avoids the conversion to a 32 bit integer, which will probably cost a few clocks.

One thing to keep in mind though, is that within the next 5 years those BOOLs will become 64 bits long as 64 bit archetecture takes hold in desktops.

Granted, 64 bit systems should be able to have more RAM anyway, but that''s not likely to affect how much RAM is in desktops anytime soon. Within 5 years I''d be surprised if the average user has more than 1-2GB of RAM.

Then again, the conversion from a byte to a 64-bit integer might take more clocks as well...so...

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

FWIW, a quick test shows that BOOLs are about 15% faster than bools in a simplistic test that passes a bool into a function, sets a bool to the value, tests it, and returns a bool and the equivalent with BOOLs.

Why?

The assembly generated does an AND of the bool with 0FFH every time it is using it but not when it it is using the BOOL.

This was with VS 6.0 optimized.

Okay, this isn''t the order of magnitude speed up that a better algorithm can sometimes give you, but...
BOOL's ought to be faster unless you are using millions of them, bool require more bit fiddling to guarantee they are either 0 or 1. int's (BOOL) are false when 0 and true otherwise (typically -1 is used for true since this sets all the bits to 1).

BOOL is a Win32 typedef, whereas bool is a distinct C++ type. Despite the apparent trivial difference, BOOL cannot be substituted for many cases where bool is truely needed.

[edited by - Magmai Kai Holmlor on May 19, 2004 10:49:56 PM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement