Where is Haskell better than C++?

Haskell is very competitive with C++, and in a few benchmarks, it is even faster. But how can Haskell possibly be faster? 

Haskell advantages over C++

It is common for languages to be compared with C++. At this point, it is an unofficial benchmark of sorts. However, it is effective and shows that most languages are slower than C, especially when you get into the higher-level ones.

Haskell shows that it has some advantages, and even if, in some cases, it is slower, the differences in speed are marginal and within a few percent of C++. There are two possible reasons for that, additionally to the main reason, the functional basement:

  • The first is optimization. The Haskell compiler has a lot of knowledge about the program and the form of types. Usually, those types are richer than the types in C++.
  • Second, there is a lot of the semantics of Haskell that give the compiler a lot of leeway or wiggle room for optimization.

One of these semantics is lazy evaluation. In C++, it’s strict evaluation, you put a function there, and it executes one line at a time. Everything on this line is going to be finished before we get to the next. In Haskell, that’s not the case. You can call something, give it a name, and it will not do anything yet. The compiler can analyze it and often figure out, “Hey, you’ve never used this. I will not do it until I know you’re in this branch.”

Or, it might never do it. The analysis gets so complex. There’s a lot of stuff you could, in theory, hand-tune if you’re very good at optimizing.

Another thing about Haskell is because it is purely functional, it can do more optimizations, like moving code around, inlining, and doing more stuff at compile time that can not be done in C++. Haskell has a broader range of maneuvers that it can do to let the code get optimized well. You can code how you read. In other words, you can create a code that is readable for another programmer, but then the compiler can transform it into something better to be executed on the machine.

Data and algorithms

Additionally, Haskell lets you use better data and algorithms. If you need a data structure in C++, it’s hard to do anything more complex than a linked list. Because it’s a higher level, Haskell can manage much more complex data structures. It gives you tools to write data structures that are known to be more efficient for specific access patterns. Check these set of examples to understand the specifics behind Haskell programming.

For example, you can replace a linked list with something else, like a tree. This way, your access becomes logarithmic, and you’ve already saved a bunch in terms of time complexity. It’s simply a matter of how much complexity you can handle. 

Of course, if someone wrote a tree in C++ and you imported that library and included it in your code, you would start competing again with Haskell. But who does that? Especially since with Haskell, you can write it yourself.

Conclusion

Despite all of this, C++ is more popular. It has been around longer, and developers know how to work with it. People have been optimizing its algorithm for years and know exactly how to make it the fastest, so naturally, they choose what they know best. When you’re dealing with more real-world problems, often you do need garbage collection and a better concept of data type and structure than C++ will give you. That’s what happens. It is not just about data structures; there are other language facilities.

The absence in Haskel of many pre-made libraries can be a disadvantage and an advantage at the same time. By loading external libraries and functions in C + +, you can not control its changes and can not guarantee the absence of bugs in the libraries, thus increasing the vulnerability. Besides, in Haskell, you can do something much more difficult in C++ – to make the environment in which the services work raise read requests at an early stage of the request and even parallelize them without the programmer having to think about it. 

So, both languages have their strong and weak sides. Each developer must decide what they are ready to sacrifice to get these advantages and which they can live without.

Last Updated on December 15th, 2023