M
Mark McIntyre
(misc stuff about x86 assembler, etc. )
Remind me what the hell this has to do with C?
Remind me what the hell this has to do with C?
Christian said:Tim Prince said:[...] All the more reason for using a compiler which can
take portable C and choose the best instruction for the target architecture.
The OP assertion, that ++i could be more efficient (when used in subscript
context), was true of gcc on the Mac I once had.
Well ok, then the Mac port of the gcc compiler sucks ass -- but it
also means that the underlying PPC must be somewhat weak not to make
this irrelevant, which I am not sure I believe.
Not for ++i vs. i++, but for *++p vs. *p++ it made a difference:
pete said:Christian said:[...] All the more reason for using a compiler which can
take portable C and choose the best instruction for the target architecture.
The OP assertion, that ++i could be more efficient (when used in subscript
context), was true of gcc on the Mac I once had.
Well ok, then the Mac port of the gcc compiler sucks ass -- but it
also means that the underlying PPC must be somewhat weak not to make
this irrelevant, which I am not sure I believe.
Not for ++i vs. i++, but for *++p vs. *p++ it made a difference:
*++p is semantically different from *p++
There's no way that the evaluation of those two expressions in code,
could generate the same machine instructions in translation.
Peter said:pete said:Christian said:[...] All the more reason for using a compiler which can
take portable C and choose the best instruction for the target architecture.
The OP assertion, that ++i could be more efficient (when used in subscript
context), was true of gcc on the Mac I once had.
Well ok, then the Mac port of the gcc compiler sucks ass -- but it
also means that the underlying PPC must be somewhat weak not to make
this irrelevant, which I am not sure I believe.
Not for ++i vs. i++, but for *++p vs. *p++ it made a difference:
*++p is semantically different from *p++
There's no way that the evaluation of those two expressions in code,
could generate the same machine instructions in translation.
I think what Christian Bau is talking about is the difference between the
following...
char *strcpy(char *s, const char *t)
{
char *p = s;
while (*p++ = *t++);
return s;
}
char *strcpy(char *, const char *t)
{
char *p = s; /* more usual is: char *p = s - 1; */
if (*p = *t) /* t--; */
while (*++p = *++t);
return s;
}
These two are semantically the same (hopefully! ;-),
pete said:In this version of strncpy, the bottom loop is my prefered
method of writing a loop that will execute as many times
as the intitial value of n, when I'm not counting clock ticks.
But after the top loop executes,
I need n to represent the number of times still left to go,
so I can't write while(n-- && *s2 != '\0'), there.
char *strncpy(char *s1, const char *s2, size_t n)
{
char *const p1 = s1;
while (n != 0 && *s2 != '\0') {
*s1++ = *s2++;
--n;
}
while (n--) {
*s1++ = '\0';
}
return p1;
}
Christian said:The second loop would be an example of making your code unreadable
in the hope of saving a few nanoseconds
Unless there's a compelling reason to null out the remainder of s1,pete said:Christian Bau wrote:
How do you figure there's a hope of saving a few nanoseconds ?
while (n--){;}
is easy for me to recoginize
as a loop that's supposed to execute n times.
That's why I like it.
Unless there's a compelling reason to null out the remainder of s1,
a single *s1 = '\0' would suffice to null terminate the string,
The second loop would be an example of making your code
unreadable in the hope of saving a few nanoseconds
(without success, for many compilers). Why not
for (; n > 0; --n)
*s1++ = '\0';
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.