为什么说Erlang比Java和C++更适合网页游戏的服务器端编程?

b5buobof  于 2022-12-08  发布在  Erlang
关注(0)|答案(3)|浏览(257)

我真的不明白,Erlang怎么会比C++更高效呢?

s1ag04yj

s1ag04yj1#

Erlang的效率远远低于C++。Erlang的最大优势是可伸缩性,而不是效率。它可以在多个CPU之间线性伸缩,并且由于其编程和通信模型,可以非常容易地在机器集群之间伸缩。
需要说明的是,Erlang的伸缩性不会 * 超过 * C++;它只是比C++更容易伸缩。非常容易。请参阅Concurrent Programming in Erlang的第5章和第6章,以获得对为什么会这样的一个非常好的解释。

x7yiwoj4

x7yiwoj42#

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

rn0zuynd

rn0zuynd3#

当Erlang被宣传为比C更好的时候,这不是关于效率的问题,而是关于错误处理和并发性的问题。用Erlang编写的服务器使用正确的OTP原则将自动地具有从错误中恢复的极好的方法。
你可以说Erlang对于程序员编写服务器应用程序更有效率,而且运行起来也更稳定。

相关问题