为什么Erlang/BEAM VM中的数学运算如此缓慢?[已关闭]

wr98u20j  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(164)

已关闭。此问题需要更多focused。当前不接受答案。
**想要改进此问题吗?**更新问题,使其仅关注editing this post的一个问题。

1年前关闭。
Improve this question
我试图理解为什么Erlang中的数学会如此缓慢,如果是这样,我可以做些什么来找出它缓慢的地方,并尝试加快它。有些人说这是因为它是一个VM,但我怀疑这是因为JVM在数学上很快,V8也是如此。其他人说这是因为它是一个不可变的语言,但是OCaml是不可变的,而且在数学方面非常快。那么是什么使Erlang在数学方面很慢呢?我如何在代码中找到它慢的地方呢?我只能想象使用DTrace,因为我对Linux/BSD工具并不了解,我也应该使用它,并且我不知道哪一个能够很好地分析VM中的代码和VM本身,以及它们是否需要不同的工具。

pkln4tw6

pkln4tw61#

Before OTP24:

BEAM does not have JIT, so typically the erlang compiler ( erlc ) outputs bytecode: Any math operation needs to access the VM registers (which are memory positions), perform the actual operation, place the result in the relevant VM register and then jump to the next instruction. This is quite slow compared to just performing the actual operation in machine code.
If you use any language that compiles directly to machine code (like C), the compiler has a lot more information about the code and the platform and thus is able to use features that speed up execution of the operations like optimizing the processor's pipeline, using vectorized instructions, placing the most accessed variables in processor's registers, optimizing memory access so that they hit the cache...
The HiPE compiler is there to compile erlang code to native, and you should use it if your program uses a lot of math. Be sure to check its limitations, though.
If the HiPE compiler is not enough, you can always code the critical math operations in C and include it .
Furthermore, you should check this question with a comparison between Erlang and C (and others) for a pure math problem
Regarding immutability, it shouldn't have any impact unless your integers are placed on the thread's heap (>60bits) because those are the only ones that require explicit memory handling.
In order to profile Erlang code, you have these tools to deal with Erlang from within.
Lastly, you can always post your snippet here and maybe we'll be able to point something.

After OTP24:

Erlang now has JIT, some reports state as much as 25% improvement.

相关问题