[erlang-questions] Is the function call always pass by value?
Motiejus Jakštys
desired.mta@REDACTED
Sat Mar 9 10:40:37 CET 2013
On Sat, Mar 9, 2013 at 10:16 AM, 饕餮 <249505968@REDACTED> wrote:
> I have do a test about the function call.
> There are some strange thing happen when I use function call to pass a
> tuple.
> The runtime won't change whatever the tuple scale size change(still smaller
> than 10k atom in it).
>
> So I wonder if the function call always pass value?
In Erlang it does not matter, because you cannot modify the tuple. AND
have no pointers. Hence I am pretty sure it does not make a difference
because pointer is passed to the stack.
> And in I my project there is a efficiency bottleneck.
> There is some function loop with passing some large record.
> simply like this
> %=============================
> dot_write_func_dup(N) ->
> X = #test{},
> statistics(runtime),
> dot_func_dup(N,X),
> {_,TotalRunTime}=statistics(runtime),
> io:format("totalruntime:~w~n",[TotalRunTime]).
> %=============================
> dot_func_dup(N,X) when N > 0 ->
> I = X#test{r500 = n},
> J = I#test{r501 = m},
> dot_func_dup(N -1 ,X);
> dot_func_dup(0,X) ->
> I = X#test{r500 = n},
> J = I#test{r501 = m}.
> %=============================
You are constructing a record (`J`) and then immediately discarding
it. Is this for purpose? You should get "unused variable `J`" warning
while compiling the module. Perhaps you meant J instead of X in
do_func_dup(N-1, X) ?
> It seems very slow with the loop.
Numbers would be really good addition to this statement: small record
vs large record.
> And we attempt to change the record loop to process dictionary.
> It's worthy to change it?
Anyway, changing a value in the record (tuple) requires to copy the
whole record (tuple)*. So yes, it will be slow. And process dictionary
will be more efficient. And think about ETS.
[*]: there are exceptions. See
http://www.erlang.org/doc/efficiency_guide/commoncaveats.html section
"3.4 setelement/3".
--
Motiejus Jakštys
More information about the erlang-questions
mailing list