4 ms·
Here's a similar, comically pathological, example: "Haskell compiles to C therefore good, handwritten C ought to upper bound performance for Haskell" main
by tel 12y ago
Here's a similar, comically pathological, example:
"Haskell compiles to C therefore good, handwritten C ought to upper bound performance for Haskell"
main :: IO ()
main = do
x <- return ([1..100000001] !! 100000000)
putStrLn "done"
As Haskell is lazy and pure it "automatically" eliminates the `x` and returns nearly immediately avoiding all work. The correspondingly written C code would do much more work despite its waste.
Of course, the human writing the C version might automatically perform this optimization, but similar things can arise in far less obvious locations in a more genuine compilation.
- jlarocco 12y agoI'm not sure that distinction matters. Any C compiler worth using will optimize out 'x' in that case. The details of why (laziness vs code analysis saying it's unused) is pretty much irrelevant. Saying "the human writing the C compiler" is an odd way to put it. It's "the human writing the Haskell compiler" deciding it shouldn't be evaluated in Haskell, too. Totally meaningless distinction.
- tel 12y agoFreudian slip, I meant "C version". It was only meant to illustrate.
- TheLoneWolfling 12y agoPlease elaborate. I don't see the difference between the person writing the compiler implementing laziness and the person writing the compiler implementing unused value removal.