On Wed, May 31, 2006 at 05:39:17PM +0000, Angus Leeming wrote: > Lars Gullik Bjønnes <[EMAIL PROTECTED]> writes: > > | > Apart from it can be shortened by two lines: > > | > for (size_t i = cur.depth(); i--; ) > > | > > | Now you're just being evil, André At least be explicit about the exit > > | condition. I *think* that your code is equivalent to: > > | > > | for (size_t i = cur.depth(); i > 0; i--) > > > > but then we want '--i' as well. > > As a matter of style, yes, probably. However, for PODs there's no efficiency > penalty to i-- vis-à-vis --i.
To add a bit more verbosity: int foo(int n) int bar(int n) int baz(int n) { { { int s = 0; int s = 0; int s = 0; int i; int i; int i; for (i = n; i--; ) for (i = 0; i != n; ++i) for (i = n; --i >= 0; ) s += i; s += i; s += i; return s; return s; return s; } } } gives with gcc -O3: foo: bar: baz: pushl %ebp pushl %ebp pushl %ebp xorl %eax, %eax xorl %eax, %eax xorl %eax, %eax movl %esp, %ebp movl %esp, %ebp movl %esp, %ebp movl 8(%ebp), %edx xorl %edx, %edx movl 8(%ebp), %edx decl %edx movl 8(%ebp), %ecx decl %edx cmpl $-1, %edx cmpl %ecx, %eax js .L60 je .L8 je .L19 .p2align 4,,7 .p2align 4,,7 .p2align 4,,7 .L61: .L9: .L20: addl %edx, %eax addl %edx, %eax addl %edx, %eax decl %edx decl %edx incl %edx jns .L61 cmpl $-1, %edx cmpl %ecx, %edx .L60: jne .L9 jne .L20 popl %ebp .L8: .L19: ret popl %ebp popl %ebp ret ret So it looks as though the 'canonical' solution ('bar') uses one more register (ecx) than the optimal version ('baz'). The only unfortunate ascpect here is that it won't work with unsigned counters as C++'s. Amdre'