Hello, I have a question about whether there is a current method to maximize code readability while minimizing computational complexity.
For example, the code:
is slower than
Provided that you do not need to retain the variable x, this method is faster ( due to memory access issues as far as I understand ). In addition, if the memory footprint of variables is a concern, this will simply overwrite the values of x rather than creating a new variable to occupy memory.
However, the latter method reduces code interpretability by obscuring the fact that, in this example, I am clearly changing to an angle using the tangent function. So, how do I balance these two apparently competing issues?
So far I have come up with four options:
For the generic operation
where newVar operates in the same way as oldVar = myFun(oldVar), but with a new name
(1) Rename the variable using intermediaries. Maybe using oldVar = myFun(oldVar); newVar = oldVar; clear oldVar; However, this lacks a certain amount of elligence and just does not sit well with me. Maybe it is the only way.
(2) Rename the variable directly. I think this gets me in hot water with the 'no dynamic variable names' credo, if it's even possible, but there is a 'rename variable' option within the workspace pane. However, even if there is a mechanism to rename the variable, similarly to the above idea it also does not necessarily increase code interpretability/compactness.
(3) Create a custom class so that the variables are handles referencing the same object in memory. oldVar and newVar point to the same object in the same way that newVar = oldVar does not create a new copy in memory until newVar is modified. Drawback: I think that will ruin a lot of codes that utilize class requirements, such as requiring inputs be 'double'. I also do not know if this will actually reduce memory loading, as I am still a novice at handle objects vs. data objects.
(4) Implement a 'description' for each variable. This does not currently exist, afaik, but would be useful more generically. If there is an initial description of 'x' that is 'distance' and then after x=atan(x) you can change the description to 'angle', that would extremely helpful for many applications. This could also be used for storing units (distance in feet vs. distance in meters). I could see creating custom classes that include descriptions in their properties, but again I think this would break class requirements for many standard functions. Maybe make them a child of the class 'double'?
(5) Suck it up and do it all with code comments.
(6) Magic, or programming tricks significantly advanced as to be indistinguishable from magic.
How do I maximize code interpretability while minimizing memory/computational cost for operations of the form x = fun(x) VS. y = fun(x).
PS. Below is a code that lets you see the slight speed advantage to writing to the same variable. Of bigger concern to me is the memory footprint. This is taken from the article linked above and here .
N = 3e3;
x = randn(N);
y = x*1.2;
x = x*1.2;