lkml.org 
[lkml]   [2018]   [Jun]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    From
    SubjectRE: [PATCH 1/3] perf alias: Remove trailing newline when reading sysfs files
    Date
    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

    \
     
     \ /
      Last update: 2018-06-15 11:10    [W:3.599 / U:0.016 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site