LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] CPU observances

[ILUG] CPU observances

P at draigBrady.com P at draigBrady.com
Wed May 19 15:17:31 IST 2004


I was bored on the train this morning and decided
to see how long it would take me to write strstr().
After a couple of mins I had:

char* strstr(const char* haystack, const char* needle)
{
     const char* hay = haystack;
     for(;;) {
         const char* p1 = hay;
         const char* p2 = needle;
         do {
             if (!*p2) return (char*) hay;
         } while(*p1++ == *p2++);
         if (!*hay++) return NULL;
     }
}

Right I compared the performance of this to glibc
and it was 46% slower. Being (over) confident in my programming
ability to produce efficient code I suspected the compiler
was generating suboptimum code (with -march=pentium4 -O3)
and so tried messing with gcc branch prediction which I
suspected might help somewhat.
     Well guess what, giving gcc a hint in the appropriate
place makes the code slightly faster than glibc! This surprised
me and confirms my observation that increasily (as hardware
gets faster and more functional) it's really pays to tweak
things, and also there is probably a LOT of easy performance
gains to be had in current software. All I did below was
to add a __likely() hint to gcc in the appropriate place:

/* __likely etc. is to provide the compiler
    with branch prediction information.
    Search for builtin_expect in `info gcc` */
#if __GNUC__ < 3
     #define __builtin_expect(foo,bar) (foo)
     #define expect(foo,bar) (foo)
#else
     #define expect(foo,bar) __builtin_expect((long)(foo),bar)
#endif
/* these macros cloned from Linux kernel */
#define __likely(foo) expect((foo),1)
#define __unlikely(foo) expect((foo),0)

char* strstr(const char* haystack, const char* needle)
{
     const char* hay = haystack;
     for(;;) {
         const char* p1 = hay;
         const char* p2 = needle;
         do {
             if (__likely(!*p2)) return (char*) hay;
         } while(*p1++ == *p2++);
         if (!*hay++) return NULL;
     }
}

On an unrelated CPU/Linux issue, it looks like 386 support will
be dropped soon due to the instructions not being available for NPTL.
sniffle...
http://sources.redhat.com/ml/libc-hacker/2004-05/msg00019.html

-- 
Pádraig Brady - http://www.pixelbeat.org
--- Following generated by rotagator ---

#list internet services on a system
netstat -lp --inet

#list active connections to/from system
netstat --inet
--



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell