| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Squares |
Hi Darin! :-)
DM> ;-) Most modern compilers probably do understand strlen. For
DM> example, I believe Pentiums have a single hardware instruction that
DM> performs the strlen function much faster than software can emulate,
DM> so you'll end up with something that looks like a function call
DM> becoming pure assembly ;->
Not in one instruction, though that is somewhat true for memcpy and memset.
DM> Can be true - but the optimiser knows some library functions...
gcc, for example, replaces memcpy and memset with its own routines for
short copies since it knows its optimized versions are likely to be faster
than the C library functions. It has different variants, each optimal for
different sizes of the memory regions it has to work on.
For strlen inside the condition of a for loop, the situation is more
complicated since the code inside the loop can modify the string, and
really complicated data flow analysis in the compiler would be needed to
prove otherwise. Especially if the code inside the loop calls code in
another compilation unit (from another file or inside some library).
strlen depends on side effects (the contents of memory when it is called),
so multiple calls to it cannot easily be optimized away.
gcc has a special attribute (pure) that one can attach to functions that
tells gcc that it does not depend on or cause side effects. This helps the
optimizer and then it *can* optimize away multiple calls with the same
argument(s).
JB>> for (n=0; target[n] != '\0' ; n++) {
DM> I prefer this one. ;-)
I would write this one as (minor difference only):
for (n = 0; target[n] != 0; n++) {
...
}
JB>> and possibly even better re-written to use a char* instead
JB>> for( s=target ; *s ; s++ ) {
DM> Yes, I like this even more ;-)
I think that's obfuscated enough to require several seconds of thought when
looking at it some time after writing it. ;)
However, I never needed to traverse a string this way in all the C programs
I have written so far.
Ciao
Pascal
--- Msged/LNX 6.1.1
* Origin: Priority override. Tears of Ra. (1:153/401.2)SEEN-BY: 633/267 270 @PATH: 153/401 307 140/1 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.