Re: Unsignedness of std::size_t
On 4/16/07 1:50 PM, in article
1176746632.900053.188670@b75g2000hsg.googlegroups.com,
"usenet@leapheap.co.uk" <usenet@leapheap.co.uk> wrote:
jeff_alexander wrote:
I could even make the case that checks are faster for unsigned indexes
because there is only one compare, not two. This is not a nit-pick:
there are cases where it matters; container classes should scale as
well as possible, even with debug assertions.
If you mean the difference between:
int i;
if ( i >= 0 && i < 1000 )
and
unsigned u;
if ( u < 1000 )
The two comparisons are functionally identical (they differ only in their
presentation). So there is little reason to expect that their compiled bits
would not also be the same.
Now, I do know that conducting research and performing experiments to
collect information for a USENET posting is frowned upon in any serious
USENET discussion (it's considered almost a form of cheating). Nevertheless,
I went ahead and wrote a C++ program that had two functions, f1() and f2()
(along with a main() routine, not shown, to exercise them both and to ensure
that they would not be optimized away to nothing):
int f1( int i)
{
if ( i >= 0 && i < 1000 )
return 1;
return 0;
}
int f2(unsigned u)
{
if ( u < 1000 )
return 1;
return 0;
}
I then compiled my program with a C++ compiler (gcc 4.01 for the x86) using
the -save-temps command line switch to generate assembly language listings.
Here is what I observed: Even with optimizations completely disabled, gcc
produced byte-for-byte identical assembly listings when compiling f1() and
f2(). So, as expected, the compiled bits for the two functions do turn out
to be the same. Perhaps somewhat more surprisingly was the assembly listing
themselves (which I have consolidate to one listing, since the two are
identical):
Unoptimized f1()/f2() assembly listing:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
cmpl $999, 8(%ebp)
ja L8
movl $1, -12(%ebp)
jmp L10
L8:
movl $0, -12(%ebp)
L10:
movl -12(%ebp), %eax
leave
ret
Note that without optimization, f1() and f2() are both equally inefficient -
performing two comparisons (against 0 and 999) instead of just one. In other
words, both routines looks like like a match for the C++ code found in
f1()'s implementation.
So one might therefore expect that the optimized compilations of f1() and
f2() would then more closely follow the leaner and apparently more efficient
code in f2()'s implementation. And, in fact that turns out exactly to be the
case:
Optimized f1()/f2() assembly listing
pushl %ebp
movl %esp, %ebp
xorl %eax, %eax
cmpl $999, 8(%ebp)
setbe %al
popl %ebp
ret
So when compiled at the highest optimization level (-O3), the assembly
listings for f1() and f2() are still identical (to each other) but quite
different from their unoptimized listings shown earlier. In the optimized
compilation, there is now only one comparison performed (against 999) - just
like the C++ code in f2() performs only one comparison. In short, the
optimized compilation of these two functions now resemble f2()'s C++
implementation.
the compiler [1] will generate the same code for both.
It will consist (given conventional processor architecture)
of one unsigned comparison followed by a conditional jump.
Had you only believed your own answer, then you would have been right :-).
[1] That is, the fantasy optimising compiler frequently
sent into battle in these newsgroups.
Actually, any run-of-the mill C++ compiler (optimizing or not) would have
won a "battle" as easy as this one.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]