Hacker News new | ask | show | jobs
by ColinWright 2327 days ago
So I just wrote the most naive version of wc I could think of in C, matching the capability of this Haskell version, and I smoked the system wc ... my code, unoptimised, was over twice as fast.

$ time wc Backups/Tera2/files.txt

  1123699   2283439 161361844 Backups/Tera2/files.txt

  real 0m2.010s
  user 0m1.964s
  sys 0m0.020s
$ time naive Backups/Tera2/files.txt

  L: 1123699
  W: 2283439
  C: 161361844

  real 0m0.864s
  user 0m0.835s
  sys 0m0.028s
Smoked !!

(See my comment elsewhere[0] for a more sensible comment)

[0] https://news.ycombinator.com/item?id=22234673

--------

Update: I just compiled with -O3 and got user time of 0.24 secs. This version is 8 times faster than the system wc.

2 comments

Just put together a naive version of wc in Go and got similar perf gains over the wc that ships with macOS.

That being said, unlike the OP, I don't think it means anything as wc is likely supporting more features than my program.

    $ time wc test.txt
     16500000 49252094 2059004431 test.txt

    real 0m5.930s
    user 0m5.491s
    sys 0m0.374s

    $ go build wc.go && time ./wc test.txt
    16500000 49252094 2059004431

    real 0m2.947s
    user 0m2.620s
    sys 0m0.299s
https://gist.github.com/felixge/aa70fc97e893a7eb0bd4c801f8f5...
I got 5.6x speedup on just wc alone with:

  export LANG=C
(obviously in both cases with prewarmed filesystem cache)
That doesn't seem to help on macOS. I suspect you're on Linux?