lkml.org 
[lkml]   [2015]   [Jun]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH 1/1] lib: small update for strlen, strnlen, use less cpu instructions
From
Date
On Tue, 2015-06-16 at 13:50 -0500, Orestes Leal Rodriguez wrote:
> Very small update to strlen and strnlen that now use less cpu
> instructions by using a counter to avoid the memory addresses
> substraction to find the length of the string.
[]
> @@ -418,12 +422,13 @@ EXPORT_SYMBOL(strlen);
> */
> size_t strnlen(const char *s, size_t count)
> {
> - const char *sc;
> + size_t sz = 0;
>
> - for (sc = s; count-- && *sc != '\0'; ++sc)
> - /* nothing */;
> - return sc - s;
> + for (; count-- && *s++ != '\0'; sz++)
> + /* empty */;
> + return sz;

That's one subtraction at end-of-string vs
a register increment for each non-zero byte.

smaller isn't worth slower.



\
 
 \ /
  Last update: 2015-06-17 00:41    [W:0.033 / U:0.112 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site