[erlang-questions] 回复: Is the function call always pass by value?

饕餮 249505968@REDACTED
Mon Mar 11 02:18:17 CET 2013


passed by reference?
This does make sense in my project!
It's that means It won't change the effect when I use a tail recursion with a record parameter or with no parameter?
If it pass by value. It may waste a lot of time to copy values...


------------------ 原始邮件 ------------------
发件人: "Robert Virding"<robert.virding@REDACTED>;
发送时间: 2013年3月10日(星期天) 上午8:48
收件人: "Motiejus Jakštys"<desired.mta@REDACTED>; 
抄送: "erlang-questions"<erlang-questions@REDACTED>; "饕餮"<249505968@REDACTED>; 
主题: Re: [erlang-questions] Is the function call always pass by value?



Yes, basically, everything, both arguments and return values, is passed "by reference". But, as you say, this is not noticeable as you can't change a value.

Robert

----- Original Message -----
> From: "Motiejus Jakštys" <desired.mta@REDACTED>
> To: "饕餮" <249505968@REDACTED>
> Cc: "erlang-questions" <erlang-questions@REDACTED>
> Sent: Saturday, 9 March, 2013 10:40:37 AM
> Subject: Re: [erlang-questions] Is the function call always pass by value?
> 
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130311/f49ef1c0/attachment.htm>


More information about the erlang-questions mailing list