Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I only have a little coding experience in C, and I remember I was told that pass by reference is more efficient than pass by value since the parameters don't need to be copied. Since there is no pass by reference in Mathematica, I'm wondering if that affects the performance of function calls, especially when dealing with large data?

share|improve this question
1  
This excellent answer by @Szabolcs probably addresses most of this question. – Leonid Shifrin Mar 8 at 1:05
1  
also this question stackoverflow.com/questions/7376032/… I see no difference in this simple test !Mathematica graphics – Nasser Mar 8 at 1:07
@Nasser Actually, the question you linked is relevant, but only tangentially. Besides, the accepted answer there promotes dangerous practice of using Unevaluated, which is a wrong thing to do from the software development perspective. Instead, Hold- attributes should be used, as I emphasized in my answer there, and then also here. – Leonid Shifrin Mar 8 at 1:12
@LeonidShifrin Yes, you are right. I do not use Unevaluated[] any more. – Nasser Mar 8 at 1:22
Thanks @Leonid, I actually forgot about that one and I was preparing to write something similar. @ All, I nominated that one for migration now, please vote if you think it's a good candidate. – Szabolcs Mar 8 at 1:47
show 4 more comments

1 Answer

up vote 6 down vote accepted

In Mathematica, this does not affect performance because copying does not take place most of the time. To put it simply, Mathematica implements copy-on-demand: it creates a copy of the data structure only if it is modified. Please see this answer for a more detailed explanation:

Does passing a variable with a large amount of data cost a lot of memory and time in Mathematica?

share|improve this answer
3  
I actually still do not see how it does not affect performance. When I pass a list of 1 GB to a function, and that function wants to modify one entry in this list, then a copy of the whole list needs to be made and the copy is modified and returned. So how is Mathematica able to copy a 1 GB (and also freeing the old list) without any performance penalty? I mean compared to pass by reference, where no copying is needed. There should be performance difference. Did malloc() and free() become so fast these days? Example !Mathematica graphics – Nasser Mar 8 at 2:14
6  
I think @Nasser makes a valid point. When one needs to modify something inside a function, passing symbol (variable) where that something is stored, by reference, greatly reduces the overhead of modification, since it is done in place. I actually also felt that this is worth mentioning back then, see my last comment to your answer on SO. – Leonid Shifrin Mar 8 at 2:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.