Erlang methods include M: F (a), M: F (a), fun, F (), and apply/. How is the call efficiency? Here is a summary of Erlang/OTP in action:
That is, the call speed of methods between modules and modules is not much different. Pay attention to the slow call speed of the metadatable method (apply call, in addition, unless it is in scenarios with high performance requirements, you do not have to worry too much about this performance difference. This has roughly the same results in the official documentation:
Here is an intentionally Rough Guide to the relative costs of different kinds of CILS. It is based on benchmark figures run on Solaris/iSCSI:
- Callto local or external functions (FOO (), M: Foo () are the fastest kind of CILS.
- Calling or applyingFun(Fun (), apply (fun, []) is aboutThree timesAs expensive as calling a local function.
- Applying an exported function (mod: Name (), apply (mod, name, []) is about twice as expensive as calling a fun, or aboutSix timesAs expensive as calling a local function.
Doclink: http://www.erlang.org/doc/efficiency_guide/functions.html
The concept is clear.
Cross-module callThis concept is commonly used in Erlang expressions:
In the first form of function CILS, exprm: exprf (expr1 ,..., exprn), each of exprm and exprf must be an atom or an expression that evaluates to an atom. the function is said to be called by using
Fully Qualified function name. This is often referred to as
RemoteOr
External function call. Doclink: http://www.erlang.org/doc/reference_manual/expressions.html#id76190
Why does the speed difference make fun include or indirectly Include pointer calls that implement this method and do not involve hash-table queries? Apply/3 must find the code implementation of funtion in hashtable, therefore, it is usually slower than calling directly or calling fun. optimization during compilation when the number of parameters is determined, the apply/3 call will be optimized to the external function call in the form of M: F (a) during compilation. take a look at the following code:
a() -> M=erlang, F=now, apply(M,F,[]).b(M,F,A) -> apply(M,F,A).c(M,F) -> apply(M,F,[a,b,c]). d(M)-> apply(M,do_something,[]).
Let's add the to_core parameter and take a look at the compiled core Erlang code:
'a'/0 = %% Line 33 fun () -> %% Line 36 call 'erlang':'now' ()'b'/3 = %% Line 38 fun (_cor2,_cor1,_cor0) -> %% Line 39 call 'erlang':'apply' (_cor2, _cor1, _cor0)'c'/2 = %% Line 41 fun (_cor1,_cor0) -> %% Line 42 call _cor1:_cor0 ('a', 'b', 'c')'d'/1 = %% Line 44 fun (_cor0) -> %% Line 45 call _cor0:'do_something' ()
When the number of parameters is determined, the call methods of apply/3 have been optimized to MFA.
For details about the call method after the Erlang code is loaded, you need to find more information. It is estimated that it is in the Code server code to be continued.