Messages in this thread | | | From | David Laight <> | Subject | RE: [PATCH 1/3] perf alias: Remove trailing newline when reading sysfs files | Date | Fri, 15 Jun 2018 09:10:52 +0000 |
| |
From: Paul Clarke > Sent: 14 June 2018 14:18 ... > > + /* Remove trailing newline from sysfs file */ > > + cp = strrchr(buf, '\n'); > > + if (cp) > > + *cp = '\0'; > > A nit, perhaps, but this will search backwards through the entire string if a newline is not found, > which is the most common case, I presume. Would it be more efficient to just look at the last > character? Something like: > i = strlen(buf); > if (i > 0 && buf[i-1] == '\n') > buf[i-1] = '\0';
Worse it will do horrid things of the output has multiple lines of text without a newline at the end.
Both strlen() and strrhr() need to scan the entire string. However since strlen is only looking for one value it should be much more efficient - especially on 64bit systems where shifts and bit operations can be used to find the 64bit word containing the first zero byte.
I suspect rtrim() has an extra data cache miss.
David
| |