Xref: news2.mv.net comp.os.msdos.djgpp:4983 From: davis AT space DOT mit DOT edu (John E. Davis) Newsgroups: comp.os.msdos.djgpp Subject: Re: Speed optimization: memcpy() or for loop ?? Date: 14 Jun 1996 17:17:39 GMT Organization: Center for Space Research Lines: 73 Message-ID: References: Reply-To: davis AT space DOT mit DOT edu NNTP-Posting-Host: aluche.mit.edu To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp On Thu, 13 Jun 1996 23:13:22 +0100, Shawn Hargreaves wrote: : I think you people are seriously underestimating the power of gcc's : optimiser. It is _good_ :-) Tricks like this may occasionally gain a slight : speed increase, but in most cases gcc will do them for you if you just : write your loop in the simple and obvious way. Making something this Many of us write code for more than just gcc. Some of the code occurs in inner loops and must be as fast as possible as well as portable. For example, some compilers do not have a memchr function or do but it is slow. I have found the following to be very fast without sacrificing too much portability: char *jed_memchr(register char *p, register char c, register int n) { int n2; register char *pmax; pmax = p + (n - 32); while (p <= pmax) { if (*p == c) return p; if (*(p + 1) == c) return p + 1; if (*(p + 2) == c) return p + 2; if (*(p + 3) == c) return p + 3; if (*(p + 4) == c) return p + 4; if (*(p + 5) == c) return p + 5; if (*(p + 6) == c) return p + 6; if (*(p + 7) == c) return p + 7; if (*(p + 8) == c) return p + 8; if (*(p + 9) == c) return p + 9; if (*(p + 10) == c) return p + 10; if (*(p + 11) == c) return p + 11; if (*(p + 12) == c) return p + 12; if (*(p + 13) == c) return p + 13; if (*(p + 14) == c) return p + 14; if (*(p + 15) == c) return p + 15; if (*(p + 16) == c) return p + 16; if (*(p + 17) == c) return p + 17; if (*(p + 18) == c) return p + 18; if (*(p + 19) == c) return p + 19; if (*(p + 20) == c) return p + 20; if (*(p + 21) == c) return p + 21; if (*(p + 22) == c) return p + 22; if (*(p + 23) == c) return p + 23; if (*(p + 24) == c) return p + 24; if (*(p + 25) == c) return p + 25; if (*(p + 26) == c) return p + 26; if (*(p + 27) == c) return p + 27; if (*(p + 28) == c) return p + 28; if (*(p + 29) == c) return p + 29; if (*(p + 30) == c) return p + 30; if (*(p + 31) == c) return p + 31; p += 32; } n2 = n % 32; while (n2--) { if (*p == c) return p; p++; } return(NULL); } -- John E. Davis Center for Space Research/AXAF Science Center 617-258-8119 MIT 37-662c, Cambridge, MA 02139 http://space.mit.edu/~davis