Nguyễn Thái Ngọc Duy <pclo...@gmail.com> writes:
> strncmp provides length information, compared to strcmp, which could
> be taken advantage by the implementation. Even better, we could check
> if the lengths are equal before calling strncmp, eliminating a bit of
> strncmp calls.
I think I am a bit slower than my usual self tonight, but I am
utterly confused by the above.
strncmp() compares _only_ up to the first n bytes, so when you are
using it for equality, it is not "we could check length", but is "we
MUST check they match to the length of the shorter string", if you
want to obtain not just faster but correct result.
Am I mistaken?
Even if you are using strcmp() that yields ordering not just
equality, it can return a correct result as soon as it hits the
first bytes that are different; I doubt using strncmp() contributes
to the performance very much. Comparing lengths before doing
byte-for-byte comparison could help because you can reject two
strings with different lengths without looking at them.
At the same time, I wonder if we can take advantage of the fact that
these call sites only care about equality and not ordering.
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html