lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Le lun. 16 nov. 2020 à 16:36, Ranier Vilela <ranier.vf@gmail.com> a écrit :
> Em seg., 16 de nov. de 2020 às 11:18, Andrew Gierth <andrew@tao11.riddles.org.uk> escreveu:
>> strcmp doesn't call strlen (in reasonable implementations) and strncmp
>> isn't necessarily any faster.
>
> False.
> Strncmp always is faster.
> Once strcmp, it will go to the end of the string, always.

Look at strcmp in the GNU C Library (glibc, used in linux systems):
https://code.woboq.org/userspace/glibc/string/strcmp.c.html

Like I explained to you earlier, strcmp stops on the first difference,
or the first \0 (end of the shorter of the two string), whichever
comes first.

So in the instance you propose changing, strncmp will *not* reduce the
number of characters compared.

strcmp("a long time ago...", "method") compares a single character, and stops.
strcmp("methodxxxxxxxxxxxx", "method") compares 7 characters, and stops.