views:

81

answers:

2

Do lfind/lsearch perform better than a typical looping solution that checks each item until it matches? Is there any special sauce/reason that these functions exist?

+2  A: 

Probably they are not more efficient that a homebrew version, maybe even a bit less since the comparison function can't be inlined.

But this is certainly not the point with them. They complete the API of the other search functions, in particular bsearch and tsearch.

Jens Gustedt
I read the source code for lfind/lsearch in eglibc-2.11.1, there are no special optimizations made at all. The inability to inline is the biggest problem just as you've stated.
Matt Joiner
A: 

Measure!

You can only know about performance by measuring. Things are definitely different on your computer and mine (I may even not have a POSIX compiant compiler cannot measure lfind myself). Thinks are different between different runs of a program.

So, if you need to know, try both the lfind and homebrew methods and measure several times.

Anyway ... C library functions may not have been written in C. If your lfind was written in Python I'd bet it would be slower than a homebrew method :)

pmg