www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/17/00:44:06

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: <Pine DOT SGI DOT 3 DOT 91 DOT 960613230307 DOT 20362B-100000 AT tower DOT york DOT ac DOT uk> <slrn4s37lj DOT avo DOT davis AT aluche DOT mit DOT edu>
Reply-To: malcolm AT manawatu DOT gen DOT nz
NNTP-Posting-Host: malcolm.manawatu.gen.nz
Mime-Version: 1.0
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019