Gavin Lambert wrote:
At 08:33 17/01/2009, Jim Idle wrote:
Right - so your one function dealing with strings is 12% of the time (ignoring the tree stuff for the moment), rather than strcmp you should use memcmp, but it would be faster to use a TRIE.

What have you got against strcmp?  It's usually implemented as a call to memcmp anyway...

It has to look for the terminator whereas memcmp does not. Instruction sets and caches these days tend to make the difference smaller than it once was, but strcmp still has to do a bit more work. When it is called 3,800,000 times, the difference is amplified of course. Strcmp is generally different assembly code than memcmp as it must check for the terminator. If you look at the source for the C versions (FreeBSD happened to be the first I came across) for algorithmic reference...

 44 {
 45         while (*s1 == *s2++)
 46                 if (*s1++ == 0)
 47                         return (0);
 48         return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
 49 }

 41 int
 42 memcmp(const void *s1, const void *s2, size_t n)
 43 {
 44         if (n != 0) {
 45                 const unsigned char *p1 = s1, *p2 = s2;
 46 
 47                 do {
 48                         if (*p1++ != *p2++)
 49                                 return (*--p1 - *--p2);
 50                 } while (--n != 0);
 51         }
 52         return (0);
 53 }

You can see there is an extra memory reference to check for the terminator, though this is probably optimized by the compiler. 

Mostly of course, these functions are implemented in assembler and some assembly versions may be able to use instructions that can perform the zero check and comparison all in one go, but generally I have not known strcmp to be implemented as a call to memcmp as memcmp does not check for a terminator so you would have to first scan for the terminator to know how many bytes to ask memcmp to compare. Things change all the time though :-)

The assembly implementations for Windows and various machines can be fond with a Google of memcmp.asm - I did not find any that call memcmp (as the call is an overhead itself of course and you cannot know that it will be in the processor cache or not).

However, the real problem is that if you need to look something up 3,800,000 times then you need a better algorithm not a  more efficient system call :-)

Jim



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---



List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to