Does pass-by-value affect the performance of function calls?



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?


Posted 2013-03-08T00:48:20.383

Reputation: 2 302

2This excellent answer by @Szabolcs probably addresses most of this question. – Leonid Shifrin – 2013-03-08T01:05:10.467

@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 – 2013-03-08T01:12:07.787

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 – 2013-03-08T01:47:50.603

You will find that despite some superficial similarities, C (and indeed virtually every other language) is a very poor model for how Mathematica actually works and many conclusions are not at all transferrable even at a conceptual level. In this case one may perhaps say that although the semantics of function calls with unheld arguments are isomorphic to passing by value, the implementation is rather different. Long story short: arguments are not copied. – Oleksandr R. – 2013-03-08T03:15:24.253

However, as it happens, Mathematica is somewhat similar to Python in this respect. I just found this answer on StackOverflow discussing Python's behavior, in case you would be interested.

– Oleksandr R. – 2013-03-08T03:33:02.983

1If you want to change the object then using HoldXXX attributes to emulate by-reference is essential. If you do not make alterations then, as others have stated, the usual evaluation mode is fine. It is similar to by-value but it does not copy expressions, instead incrementing a reference counter and decrementing it when finished. – Daniel Lichtblau – 2013-03-08T15:15:23.337

Thanks all for the great answers and comments! Your comments and answers makes it possible for me to learn about concepts I have never heard of, such as immutability, hashability,pass-by-value via code injection etc. Thanks. – user0501 – 2013-03-08T19:12:01.047



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?


Posted 2013-03-08T00:48:20.383

Reputation: 213 047

6I 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 – 2013-03-08T02:19:44.623