Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing arrays, without overhead (preferably "by reference"), to avoid duplicating complex code blocks, in matlab?
    primarykey
    data
    text
    <p>I have complex code blocks, in a Matlab script, that act on large, non-sparse arrays. The code performs many write operations to random elements in the arrays, as well as read operations. The identical code must execute against different (large) arrays (i.e., the same code blocks, except for different array variable names).</p> <p>I do not want to have long, duplicated code blocks that differ only in the array names.</p> <p>Unfortunately, when I create a function to perform the operations, so that the code block appears only once, the performance slows down by a factor of 10 or more (presumably due to the copying of the array). However, I do not need the array copied. I would prefer to "pass by reference", so that the purpose of the function call is ONLY to avoid having duplicated code blocks. There seems to be no way to avoid the copy-on-write semantics, however.</p> <p>Also, it is impossible (so far as I understand) to create a script (not a function) to achieve this, because the script must contain identical variable names as the calling script, so I would need a different script for every array on which I wish to run the script, which gains nothing (I still would have duplicated code blocks).</p> <p>I have looked into creating an alias variable name to "substitute" for the array variable name of interest, in which case I could call a script and avoid duplicated code. However, I cannot find any way to create an alias in Matlab.</p> <p>Finally, I have attempted writing a function that utilizes the <code>evalin()</code> function, and passing the <em>string</em> name of the array variable to this function, but although this works, the performance is also vastly slower - about the same as passing the arrays by value to a function (at least a 10 times decay in performance).</p> <p>I am coming to the conclusion that it is impossible in Matlab to avoid duplicating code blocks when performing complex operations on non-sparse arrays, in the effort to avoid the ghastly overhead that Matlab seems to present using any possible technique of avoiding duplicated code blocks.</p> <p>I find this hard to believe, but I cannot find a way around it.</p> <p>Does anybody know of a way to avoid duplicated code blocks when performing identical intricate operations on multiple non-sparse arrays in Matlab?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO[This answer](http://stackoverflow.com/a/3427461/855026) might help. Matlab uses "copy on write" semantics, so matrices are in fact passed "by reference" to functions. I'm afraid I don't know why you are experiencing such a dramatic drop in performance though. Does your function change values in the input matrices?
      singulars
    2. COAs I noted, I am performing many write operations to random elements in the arrays. I understand that this is why Matlab is, sadly, triggering "copy on write" in extensive fashion in my case. The reason for the dramatic drop in performance seems clear: Large, non-sparse arrays are being frequently (and unnecessarily) copied. The copies are unnecessary, as I want the writes to act on the *original* arrays. However, as noted, I cannot find any way to "pass by reference" *even* in the case of write semantics (i.e., I do *not* want "copy on write" semantics).
      singulars
    3. COI am, indeed, giving the input and output variables the same name. Are you absolutely certain that this completely prevents copy-on-write semantics? I am seeing a tremendous decrease in performance for *identical* code (not a single line changed), just by wrapping the code in a function call (AND being certain to use the same variable names within the function for the input and output variables).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload