Live data from Hacker News

When Haskell is Faster than C

paulspontifications.blogspot.com

81–90 of 119 posts

Re: When Haskell is Faster than C

#81

I want to point something nobody ever talk about : Efficient data containers. For instance, Haskell and C++ come in standard with a lot of these (maps, sets, various linked lists, ...) whereas the C standard doesn't come with anything like this. Moreover, type parameters and templates from these languages make usage of such structures easier and safer. I've seen some C codes where programmers has used sub-effective c…

Haskell maps, being persistent, aren't anywhere close to efficient for mutable maps.

Re: When Haskell is Faster than C

#82
>Conventional wisdom says that no programming language is faster than C, and all higher level languages (such as Haskell) are doomed to be much slower because of their distance from the real machine.

No: conventional wisdom just says that no higher level programming language is consistently faster than C AND/OR better to reason about with regards to memory consumption and runtime behaviour.

(Conventional wisdom also adds fortran, forth, Ada and C++ in the same "speedy" category).

Conventional wisdom adds that micro-benchmarks of some BS outlier examples (Java where the JIT can take advantage of some known condition to do something clever, etc) do not matter in real life programs, which are far more complex.

Conventional wisdom also adds that C to be speedier it doesn't even have to be highly optimized or carefully crafted by some C programming wizard or anything. Merely avoiding gross mistakes (like using an algorithm of the wrong complexity for the job) will do.

Conventional wisdom concludes that writing in your high level language in an unconventional (non idiomatic) way to get to "as fast a C" speeds is bullshit too, because it doesn't represent idiomatic (and far more common) high level language use.

Re: When Haskell is Faster than C

#83
post #70

I'm afraid the argument that C+ASM is always faster is flawed in reality. Pure ASM, with a bit of C thrown in, maybe, but this is just as impractical for complex codes as C itself is. It is well known that for numerical codes Fortran beats the pants off C. Why is this? Because the structure of C programs proves difficult to optimise automatically. Indeed the C committee attempted to address one of the main problems b…

The Rust compiler was also written in OCaml (until it could be self-hosted in Rust itself).

Re: When Haskell is Faster than C

#84
post #73

Earlier quoted context omitted.

I guess I will be your essay-writing dissenter. C is a high-level language with fewer features than many other languages, is not necessarily the engine behind other languages, has the problem of its programs poorly implementing a percentage of what other high level languages are capable of doing quickly and securely, and provides slow and troublesome memory allocation out-of-the-box. When comparing the speed of opera…

> Let's be honest: C is no "closer to the metal" than other high level languages This is dead wrong, and your links do not support it. This is exactly the kind of statement that gets me grumpy. Your link illustrates that an aggressive C optimizer can collapse a chunk of C code down to something smaller and simpler than the original code. This is true. But what you said is that C is "no closer to the metal" than other…

Seconded. I'd add that languages like Python hide an enormous amount of not-close-to-the-metal-ness in every single statement, because every single statement has the implicit context "Interpreter, please interpret this string relative to your potentially complex internal state".

Haskell is only slightly less prone to this, despite being compiled, since every expression by default becomes a request to instantiate a thunk in the runtime's evaluation tree. Yes, you can contort yourself to avoid this, but at that point you are imperatively programming an expression tree evaluator. This can be fun and rewarding, but it's not that different from scripting a Python interpreter, and it's certainly much further from the metal than C.

Re: When Haskell is Faster than C

#85
1) There is the C++ language. Will you ever remember it? Stop comparing everything to C — it is an oversimplified outdated language with specific uses for low-level system programming where you don't need complex data structures or high-level application logic.

2) If you summarize all the benchmarks comparing C to other languages, it turns out that the C language is one of the slowest. Of course, that's hardly the case. It's just the dudes who did the benchmarks suck in C/C++ and suck in programming in general.

3) Haskell makes you 10 times more productive without microoptimizing? ORLY? Try doing some big enough data manipulation with C++ 11 & boost vs Haskell. In C++ you're done with the task as long as you get decent performance with the simplest naive very high-level code, whereas in Haskell you're in the beginning of your optimization journey as the simplest code is not good enough, and after optimization you end up with much more complex, cluttered and hard to understand code than in C++ and its performance is still comparable to the naive C++ version if you're lucky.

Re: When Haskell is Faster than C

#86
post #73

Earlier quoted context omitted.

I guess I will be your essay-writing dissenter. C is a high-level language with fewer features than many other languages, is not necessarily the engine behind other languages, has the problem of its programs poorly implementing a percentage of what other high level languages are capable of doing quickly and securely, and provides slow and troublesome memory allocation out-of-the-box. When comparing the speed of opera…

> Let's be honest: C is no "closer to the metal" than other high level languages This is dead wrong, and your links do not support it. This is exactly the kind of statement that gets me grumpy. Your link illustrates that an aggressive C optimizer can collapse a chunk of C code down to something smaller and simpler than the original code. This is true. But what you said is that C is "no closer to the metal" than other…

Apples and oranges yet again. The original article is about Haskell; an entirely different beast from Python. Haskell compiles down to machine code, not byte code.

Re: When Haskell is Faster than C

#87
post #35
post #13

Earlier quoted context omitted.

Its more a matter of trying to nix the "Haskell would be nice, but its too slow to be practical" line that many see as a killer.

Does Haskell really have a reputation for being slow? In a world where Ruby is the de facto standard for startups, I wouldn't think Haskell would have anything to worry about.

It does. A lot of people have written ignorant blog posts whereby they show their first "real" Haskell program is extremely slow compared to their heavily optimized C version (with years of experience behind it).

A cursory glance at the code, however, reveals that their Haskell program is using linked lists of linked lists of boxed arbitrary precision integers while the C version uses a 2D array of ints.

Okay, perhaps that's a bit of an exaggeration but you get the idea.

Re: When Haskell is Faster than C

#88
post #70

I'm afraid the argument that C+ASM is always faster is flawed in reality. Pure ASM, with a bit of C thrown in, maybe, but this is just as impractical for complex codes as C itself is. It is well known that for numerical codes Fortran beats the pants off C. Why is this? Because the structure of C programs proves difficult to optimise automatically. Indeed the C committee attempted to address one of the main problems b…

It's not actually the structure of C programs, it's the guarantees the language offers. So only about the first paragraph of your rant is right.

Fortran had almost no memory aliasing, explicit global accesses, and offered almost unbridled implementor freedom. As long as the operations got done, it didn't care what happened behind the scenes.

None of the rest of the things you talk about matter when it comes to optimizing C, to be honest. If you gave me no aliasing by default and explicit globals, I probably could do near as well as fortran (though it would take significantly more analysis) in terms of loop transformations.

Note that "full alias analysis" is statically undecidable. When you say cubic order, you are thinking of Andersen's subtyping based algorithm. There are unification based algorithms that are almost linear time (inverse ackermann).

At this point, we have scaled these algorithms almost as far as you can on a single computer. You can do context insensitive andersens on many million LOC without too much trouble.

Context-insensitive unification points-to can scale to whatever size you like.

Context sensitive unification based algorithms do quite well in practice with 10 million LOC + codebases.

The main reason you don't see unification based algorithms used often in free compilers is because the entire set of algorithms are covered by patents owned by MS Research.

As a final note, note that C++ does not really help optimization in practice, it often hurts it.

It is very hard to teach a pointer analysis algorithm about virtual calling. Most compilers treat them like function pointers that get type-filtered, and do some form of class hierarchy analysis to limit the number of call graph targets, or try to incrementally discover the call graph. It's a bit of a mess.

On the other hand, straight function pointers get resolved either context-sensitively or insensitively.

In fact, C++ makes type based aliasing a lot worse due to placement new being able to legally change the type of a piece of memory, which is very hard to track over a program.

Even outside the realm of alias analysis, C++ involves a lot more structures, which means a lot more time has to be spent trying to get pieces back into scalars, or struct splitting, or something, so that you don't end up having to fuck around with memory every time you touch a piece of the structure.

I could go on and on.

In short: Any of C++'s lower level optimization advantages come from less pointer usage by programmers, not language guarantees.

At the high level, it's from better standard implementations and common usage idioms.

In any case, high level languages, particularly those with memory objects (not real pointers, just memory objects, like Java) usually solve none of the pointer/alias analysis related problems. You are still stuck with the same pointer analysis algorithms.

For example: The only nice thing about java's memory system is that doing structure-field sensitive pointer analysis can only help, whereas in C it can hurt, due to some weirdness.

It's just nobody usually gets around to doing pointer analysis on the higher level languages (because it's harder and offers no particular benefit), they lower their language to an IR that already has a good algorithm in it.

Just in case you were wondering, i'm not talking out of my ass. I wrote GCC's first set of high level loop optimizations, and also, it's pointer analysis.

Re: When Haskell is Faster than C

#89

These "faster than C" claims are almost always embarrassing (usually involving C code that would easily win if it were as aggressively optimized as the high-level language) but that's almost not the point. The real point is the larger narrative. The subtext of these posts is what we are really arguing about. So let's just duke that out directly. High-level language fans have a point, which is that high-level language…

I wish I could up vote this 10x. When folks were saying "Oh with HotSpotUltimateOptoMix III Java is going to be faster than C code!" or whatever the optimizer de-jour was I would sigh. We have very much reached a stage where there are computers which can dedicate way more cycles to your effort than you need to get it done in time, we can spend this "surplus" on making writing code easier, and that is a huge win. Enjo…

These are also people who don't understand you can JIT whatever language you like.

It's not common to JIT C or C++, but you could, and gain all of the profiling/whatever advantages. It's just not commonly done because the end result of a C/C++ compiler is "fast enough". Plus, JIT's have a weird set of tradeoffs. You'd really need a hybrid model where you did expensive high level loop transformations statically, then left the IR alone and JIT'd it[1]. This whole set of ideas quickly gets into TeNDRA/ANDF territory, however.

[1] Of course, profiling at runtime through the JIT may discover some more places to loop transform.

Re: When Haskell is Faster than C

#90
post #2

A similar anecdote from my own experience: Chesspark had a web and a win32 native client. It kept track of your friends with a roster (the underlying stack was based on XMPP). As a way to make new users feel welcome, I and a few coworkers were added to all new user's roster (like MySpace Tom). It wasn't long before this overwhelmed our clients. Each client was written by a different developer. One was in JavaScript,…

In general, JavaScript wouldn't take less time to develop and especially less time to fix than a modern C++ 11 / boost code.

That's a stupid myth ("very slow C++ development times") from the guys who suck at programming.

Post reply on HN