TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Destroying C with 20 lines of Haskell: wc

27 pointsby develop7over 5 years ago

19 comments

notacowardover 5 years ago
This doesn&#x27;t seem to be comparing anything like the same thing. Does the Haskell version <i>really</i> do the same thing as the C version? Does it handle all of the same error cases, providing the same quality of error messages if they occur? Does it handle localization? If not, that makes the comparison <i>very</i> skewed, as unhammer already pointed out. Sure, if you strip out all of the things that the people who wrote wc actually spent all that time on then you can go faster, but failing to note the differences is simply dishonest.
评论 #22235444 未加载
评论 #22235150 未加载
评论 #22235140 未加载
评论 #22235839 未加载
评论 #22235172 未加载
评论 #22235459 未加载
评论 #22235141 未加载
评论 #22235346 未加载
ColinWrightover 5 years 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 <i>smoked</i> the system <i>wc</i> ... my code, unoptimised, was over twice as fast.<p>$ time wc Backups&#x2F;Tera2&#x2F;files.txt<p><pre><code> 1123699 2283439 161361844 Backups&#x2F;Tera2&#x2F;files.txt real 0m2.010s user 0m1.964s sys 0m0.020s </code></pre> $ time naive Backups&#x2F;Tera2&#x2F;files.txt<p><pre><code> L: 1123699 W: 2283439 C: 161361844 real 0m0.864s user 0m0.835s sys 0m0.028s </code></pre> Smoked !!<p>(See my comment elsewhere[0] for a more sensible comment)<p>[0] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22234673" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22234673</a><p>--------<p>Update: I just compiled with -O3 and got user time of 0.24 secs. This version is 8 times faster than the system wc.
评论 #22235706 未加载
评论 #22235807 未加载
ColinWrightover 5 years ago
Not being great at reading Haskell, I have some questions I was hoping people here could answer:<p>* Does this cope with different whitespace, such as tabs?<p>* Does this cope with different settings of locale?<p>* Does this include the option of the &quot;longest line&quot;?<p>* Does this perform the character counts?<p>I&#x27;m pretty sure wc does all these, and that stripping them out would make it faster. If this Haskell version doesn&#x27;t do that, and yet still compares against a fully-featured version of wc, the comparison hardly seems fair.
评论 #22234786 未加载
评论 #22235195 未加载
mkupover 5 years ago
But how would Haskell version of wc compare with C version of wc running with LC_ALL=C environment variable? UTF-8 locale is much slower than C locale in coreutils, it&#x27;s a well-known fact, and their Haskell version of wc is already using fixed 8-bit characters.
评论 #22235504 未加载
megousover 5 years ago
Write me a haskell program that will init DRAM on PinePhone, setup PMIC and eMMC, load 25MiB of linux kernel and initramfs from eMMC to DRAM and will fit in hard limit of 32kB, and will do all of this in 300ms max.<p>Then I&#x27;ll consider C destroyed.
GlitchMrover 5 years ago
Destroying Haskell with 1 line of C: <a href="https:&#x2F;&#x2F;www.ioccc.org&#x2F;2019&#x2F;burton&#x2F;prog.c" rel="nofollow">https:&#x2F;&#x2F;www.ioccc.org&#x2F;2019&#x2F;burton&#x2F;prog.c</a>
tsukurimashouover 5 years ago
I don&#x27;t think it is very &quot;hard&quot; to &quot;destroy&quot; most of these programs, they were written a long time ago, they evolved with backwards compatibility or portability in mind, these can run on pretty much any system. It does seem a bit unfair to compare it to a quickly hacked together program that you test against one use case.<p>Like the other said, the second article will probably be more interesting.
评论 #22235365 未加载
quadrifoliateover 5 years ago
I would really like to see fewer of these clickbait post titles. The author honestly admits here (props!) <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22235536" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22235536</a> that they only used the title because it encourages discussion.<p>In that case, I would say that it&#x27;s up to the community to not take clickbait titles like this seriously if we want to encourage reasoned, detailed content rather than borderline flaming with words like &quot;destroy&quot; and &quot;smash&quot;. I personally don&#x27;t enjoy this new Buzzfeed style of technical post at all.<p>Therefore, I have some comments about the actual code here, but going to keep them to myself so I don&#x27;t encourage more people to follow OP&#x27;s example.
bobowzkiover 5 years ago
I don&#x27;t like these &quot;Destroying C&quot; titles.
评论 #22235536 未加载
jeen02over 5 years ago
Ah, the weekly &quot;I beat C using X language&quot;.<p>I don&#x27;t understand why LOC was even brought up. You can put all your code on a single line in most languages. The Github code linked even has 26 lines of Haskell which makes this even more nonsense.
mhh__over 5 years ago
Not unimpressive, but this a bit of a hot take. If I&#x27;m reading it right, a single file benchmark (Big O who?) against a fully fledged production C program is not a complete measurement
throwawa66over 5 years ago
Related: I have an F# snippet i run from Linqpad to keep or remove lines (based on keywords) from huge log files and it takes just a few seconds to breeze through the log and it’s done
iainmerrickover 5 years ago
I take it this is “destroying” in the sense of presidential debates or late-night satirical monologues, i.e. diverting but meaningless.
TickleSteveover 5 years ago
He is comparing a multi-threaded haskell version against a single-threaded C version???<p>&quot;...There’s also a parallel version that relies on the monoidal structure of the problem a lot, and that one actually beats C&quot;<p>coreutils wc is single-threaded, just checked.
评论 #22235212 未加载
评论 #22235205 未加载
divanover 5 years ago
Happy 33rd anniversary of Haskell devs claiming to destroy C.
deathnotoover 5 years ago
os: CentOS Linux release 7.7.1908 (Core) cpu: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz<p>Average time for wc on the same test file: 0.8 seconds
deathnotoover 5 years ago
Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz CentOS Linux release 7.7.1908 (Core)<p>average time: 0.8 seconds
jstimpfleover 5 years ago
&gt; So we’ve managed to just smash a C program that was looked at by thousands of eyes of quite hardcore low-level Unix hackers over a few decades. We did this with a handful of lines of pure, mutation-less, idiomatic Haskell, achieving about 4 to 5 times of throughput of the C version and spending less than an hour on all the optimizations.<p>I&#x27;ve done many very arrogant things in my life, because I&#x27;ve been a strange guy with lack of self-esteem who doesn&#x27;t have a clue how many things he doesn&#x27;t know. I hope I&#x27;ve never been <i>this</i> arrogant, though.
评论 #22235483 未加载
评论 #22235352 未加载
评论 #22241601 未加载
b-3-nover 5 years ago
The post is very insightful and well written. The title is provocative which is very common nowadays. However, there should be a &quot;serious&quot; section that puts things into perspective. As others have pointed out it leaves a bit of a taste otherwise.
评论 #22235415 未加载