Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!news.cs.indiana.edu!arizona.edu!arizona!dave From: dave@cs.arizona.edu (Dave Schaumann) Newsgroups: comp.lang.c Subject: Re: Efficient STRing CoMPares? Message-ID: <1193@caslon.cs.arizona.edu> Date: 17 Mar 91 00:41:05 GMT References: <1991Mar15.142821@mars.mpr.ca> <15486@smoke.brl.mil> Organization: U of Arizona CS Dept, Tucson Lines: 26 In article <15486@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: |In article <1991Mar15.142821@mars.mpr.ca> stone@mars.mpr.ca (Darren Stone) writes: |>Does anyone know if there's a better (!?) way to compare |>strings than with strcmp()? |>I'm looking for any speed advantage I can get! | |#define StrEq( a, b ) (*(a) == *(b) && strcmp( a, b ) == 0) /* UNSAFE */ By "unsafe", I assume you mean it will likely crash on a NULL pointer. I wouldn't be a bit suprised if strcmp() chokes on a NULL pointer, too. But the main point I want to make is that this really buys you little unless you are working under 2 conditions: - strcmp() is not inlined - function calls are expensive I think you'll have to agree that any professional C compiler worth the name would almost certainly inline *at least* the str???() functions; given an environment where function calls are expensive. Also, if you tend to compare equal successfully a lot, this will actually be slower. -- Dave Schaumann | dave@cs.arizona.edu | Short .sig's rule!