Xref: news2.mv.net comp.os.msdos.djgpp:5056 From: malcolm AT manawatu DOT gen DOT nz (Malcolm Taylor) Newsgroups: comp.os.msdos.djgpp Subject: Re: Speed optimization: memcpy() or for loop ?? Date: Sun, 16 Jun 1996 22:37:15 GMT Organization: Grafik Software Lines: 45 Message-ID: <4q265s$eos@news.manawatu.gen.nz> References: Reply-To: malcolm AT manawatu DOT gen DOT nz NNTP-Posting-Host: malcolm.manawatu.gen.nz Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp davis AT space DOT mit DOT edu (John E. Davis) wrote: >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) > { instead do this: if(*p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c || *p++==c) return p-1; > } > > n2 = n % 32; I'd put n2=n&31, but that's just me (yes I'm an ex-Borland user :). > while (n2--) > { > if (*p == c) return p; > p++; > } > return(NULL); >} This may turn out to be faster. I haven't tried, and gcc's optimizer may give you exactly the same results. Malcolm