object.opEquals return type

Dan murpsoft at hotmail.com
Tue Feb 26 15:16:58 PST 2008


JMNorris Wrote:

> Edward Diener <eddielee_no_spam_here at tropicsoft.com> wrote in
> news:fps7es$1uh4$1 at digitalmars.com: 
> 
> > If one were to carry the efficiency argument further, than the other 
> > possible logical conclusion is that a 'bool' should be an 'int' ( 
> > constrained to be of value either 0 or 1 ) rather than a 'byte'. But
> > to justify a poor design decision based purely on efficiency is
> > exceedingly silly.
> 
> If I understood the discusion in the previous link correctly, it was
> (at least mostly) the constraint to 0 or 1, not the smaller size of the
> return, that was less efficient.  Returning an int may still be silly (the 
> mere existance of this thread suggests that it is at least confusing), but 
> making a bool the size of an int won't solve the efficiency concern.
> 
> -- 
> JMNorris

FYI making a bool a bit is not efficient in memory or time.  The code produced ends up having to "and 0x0000_0001" in order to mask the bit value; which takes 5 bytes on top of the value and the mov instruction.

Making a bool a byte may or may not be efficient in time and space complexity, depending on how it gets used.  The x86 normal 4-byte register instructions were the most optimized for, and in most cases a byte form can be used but it takes an extra byte to tell the CPU to use the byte operand.

There are two or three particularly useful byte-operand instructions.  In my opinion it would be a considerable waste to set up boolean as a byte just to use these.  Most compilers ignore them, and I tend to agree with that.  However, if you ever hand code assembler they're quite useful.

Regards,
Dan



More information about the Digitalmars-d mailing list