Why is Erlang said to be more suited for server si

2019-03-10 19:20发布

问题:

I don't really understand, how can Erlang be more efficient than C++?

回答1:

Erlang is far less efficient than C++. Erlang's big strength is scalability, not efficiency. It will linearly scale across multiple CPUs and, due to its programming and communications model, will very easily scale across machine clusters.

Just to be clear, Erlang won't scale more than C++; it just scales more easily than C++. A lot more easily. See chapters 5 and 6 of Concurrent Programming in Erlang for a very good explanation of why this is so.



回答2:

I can see a few reasons for this:

  • Erlang is designed for Concurrency
  • Erlang is designed for Distributed Systems
  • Erlang is designed for Soft Real Time Systems
  • Erlang is designed for Availability

However, it's not good for number crunching, but it has good availability for interfacing with C and C++ and other languages. Use the right tool for the right job.

Desined for Concurrency

Erlang is a concurrent oriented programming language, and are well suited for applications that can be highly parallellized i.e. game servers. Erlang processes are much more light-weight and has good performence in process communication. This means that an application implemented in erlang can have many more processes than an application in C++ can have threads. Also see my question Technically why is processes in erlang more efficient than OS threads.

Designed for Distributed Systems

Erlang has also built in features that make the programmer more productive when dealing with distributed system. There is built in language primitives for sending and receiving messages between processes, and it is used the same way if the process is located on another core or computer. Also the programmer doesn't has to deal with marshalling and serialization when messages are sent between processes, that is built-in in the language.

Designed for Soft Real Time Systems

Erlang is designed for soft real time systems, and that is useful when doing game-servers. Compared to C++ it has built in memory management which will be much more productive for the programmer. C++ and malloc will suffer from problems when using many threads, and may be a bottleneck (See the presentation Erlang SMP support - behind the scenes at (14:00)). Compared to Java, Erlang's garbage collection is done per process (a much smaller unit) and that will be useful in a real time system.

Designed for Availability

Erlang is designed for Telecom applications where availability is critical. One of the availability features is that applications can be updated at runtime, with hot code swapping. This can be useful if you want to update your game server while it's still online.

I would recommend to see this presentation: Erlang - Software for a concurrent world



回答3:

It is not about efficiency when Erlang is promoted as better than C. It's about error handling and concurrency. A server written in Erlang using proper OTP principles will automatically have excellent ways of recovering from errors.

You could say that Erlang is more efficient for the programmer to write a server application. And when it is running it will be more stable.