www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: String compare performance

reply Kagamin <spam here.lot> writes:
You need only match 3-char strings?
Try this hack

bool cmp3c(string a, string etalon) pure
{
  if(a.length!=3)return false;
  return *cast(short*)a.ptr==*cast(short*)b.ptr && a[2]==b[2];
}
Nov 27 2010
next sibling parent Kagamin <spam here.lot> writes:
or

bool cmp3c(string a, string b) pure
{
  if(a.length!=3)return false;
  return *cast(char[3]*)a.ptr==*cast(char[3]*)b.ptr;
}

llvm.memcmp should rock with lengths known at compile time.
Nov 27 2010
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Kagamin:

 You need only match 3-char strings?
 Try this hack

I agree there are are ways to speed up the code, probably the D #3 code is fast enough. But while some things are better left to Phobos, comparing short strings efficiently is the compiler's job, not mine (and I am not sure your trick works in C99). Bye, bearophile
Nov 27 2010
prev sibling parent =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Kagamin wrote:
 You need only match 3-char strings?
 Try this hack
=20
 bool cmp3c(string a, string etalon) pure
 {
   if(a.length!=3D3)return false;
   return *cast(short*)a.ptr=3D=3D*cast(short*)b.ptr && a[2]=3D=3Db[2];
 }

Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Nov 28 2010