logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: b2a3ecac944ef4b306c593a92d12e644b73f7170
parent dc34ba2caa8e2ac4876f0b609560a3220a7dc4e7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  8 May 2025 16:20:11 +0200

cmd/wc: optimise -l without -w

With a somewhat big file (lines words bytes): 5984288 19738516 150262272

        Executable: /bin/wc
        Real time  : 0.310000
        User time  : 0.269183
        System time: 0.040176

        Executable: /opt/gnu/bin/wc
        Real time  : 0.040000
        User time  : 0.002972
        System time: 0.035669

        Executable: ./cmd/wc
        Real time  : 0.120000
        User time  : 0.079055
        System time: 0.039027

Note:  GNU wc(1) uses assembly, so this is likely about the best one
can get via simply using C, which at least should mean consistent
performance across architectures.

Diffstat:

Mcmd/wc.c12+++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/cmd/wc.c b/cmd/wc.c @@ -67,7 +67,7 @@ wc_file_bytes(int fd, char *filename) off_t bytes = 0, lines = 0, words = 0, wordlen = 0; ssize_t nread = -1; - if(FIELD_MATCH(wc_opts, WC_OPT_L) || FIELD_MATCH(wc_opts, WC_OPT_W)) + if(FIELD_MATCH(wc_opts, WC_OPT_W)) { while((nread = read(fd, buf, WC_BUFSIZ)) > 0) { @@ -94,6 +94,16 @@ wc_file_bytes(int fd, char *filename) } } } + else if(FIELD_MATCH(wc_opts, WC_OPT_L)) + { + while((nread = read(fd, buf, WC_BUFSIZ)) > 0) + { + bytes += nread; + + for(ssize_t i = 0; i < nread; i++) + if(buf[i] == '\n') lines++; + } + } else { struct stat status;