coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH] Speedup wc -l


From: Bernhard Voelker
Subject: Re: [PATCH] Speedup wc -l
Date: Tue, 24 Mar 2015 08:34:16 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

On 03/23/2015 01:03 PM, Pádraig Brady wrote:
> It just occurred to me that we could simplify from 3 to 2 loops,
> while also making the code more adaptive to the input,
> by simply determining the average line length per block.


great idea!

> I'll push the attached later.

just 2 minor, almost readability notes:

As you introduced the 'end' variable, why not using it
in the memchr() case, too?

@@ -290,7 +290,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, 
off_t current_pos)
           else
             {
               /* memchr is more efficient with longer lines.  */
-              while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
+              while ((p = memchr (p, '\n', end - p)))
                 {
                   ++p;
                   ++lines;

I'd favor multiplication by 15 on the other side, thus avoiding
to have to check for DIV-By-ZERO manually:

@@ -303,7 +303,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, 
off_t current_pos)
              FIXME: This line length was determined in 2015, on both
              x86_64 and ppc64, but it's worth re-evaluating in future with
              newer compilers, CPUs, or memchr() implementations etc.  */
-          if (lines == plines || (bytes_read / (lines - plines) > 15))
+          if ((lines - plines) * 15 <= bytes_read)
             long_lines = true;
           else
             long_lines = false;

Thanks & have a nice day,
Berny



reply via email to

[Prev in Thread] Current Thread [Next in Thread]