| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Squares |
Hi Pascal.
14-Apr-04 15:50:16, Pascal Schmidt wrote to Darin McBride
PS> Hi Darin! :-)
DM>> ;-) Most modern compilers probably do understand strlen. For
DM>> example, I believe Pentiums have a single hardware instruction
DM>> that performs the strlen function much faster than software can
DM>> emulate, so you'll end up with something that looks like a
DM>> function call becoming pure assembly ;->
PS> Not in one instruction, though that is somewhat true for memcpy
PS> and memset.
"repz scasb" is technically two instructiuons and is the basic engine
behind an implementation of strlen,
PS> it has to work on
PS> For strlen inside the condition of a for loop, the situation is
PS> more complicated since the code inside the loop can modify the
PS> string, and really complicated data flow analysis in the compiler
PS> would be needed to prove otherwise. Especially if the code inside
PS> the loop calls code in another compilation unit (from another file
PS> or inside some library)
PS> strlen depends on side effects (the contents of memory when it is
PS> called), so multiple calls to it cannot easily be optimized away
PS> gcc has a special attribute (pure) that one can attach to
PS> functions that tells gcc that it does not depend on or cause side
PS> effects. This helps the optimizer and then it *can* optimize away
PS> multiple calls with the same argument(s)
JB>>> for (n=0; target[n] != '\0' ; n++) {
DM>> I prefer this one. ;-)
PS> I would write this one as (minor difference only):
PS> for (n = 0; target[n] != 0; n++) { ... }
the extra " != 0 " ay result in unwanted code but optimising compilers
should be able to cut it out...
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 ;-)
PS> I think that's obfuscated enough to require several seconds of
PS> thought when looking at it some time after writing it.
Use it 20 times and it'll not be at all hard to understand next time you
encounter it.
PS> However, I never needed to traverse a string this way in all the C
PS> programs I have written so far
I think I wrote a simple uudecoder that used that construct.
PS> Ciao Pascal
-=> Bye <=-
---
(3:640/1042)
* Origin: You think "I'm no fool!" but I am! - Spike MilliganSEEN-BY: 633/267 270 @PATH: 640/1042 531 954 774/605 123/500 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™.