How Are Calculations Done for Variable That's Inclusive of Itself?

Consider the following simple scenario:
x=x+1
In terms of computational efficiency and speed, I'm curious how this calculation is handled. x is inclusive of itself and overwriting itself with a new value. So I assume x has to first be moved to RAM to then complete the calculation and subsequently store the new value. I have a very large script, with large matrices, many loops, and lots of similar calculations. The script takes a long time to run and I'm wondering if this is a bottleneck that slows down the script. An alternative might be as follows, but I'm not sure if I'd gain anything.
temp=x
x=temp+1
In this latter case, x is not inclusive of itself in the calculation, so I wonder if this is more efficient.
I'd appreciate any input.
Thanks,
M Ridzon

3 Commenti

Run your code inside the profiler to see exactly which line (or lines) causes the slowdown.
Can you elaborate? I've never run a code inside the profiler. So I wouldn't know how or where to do that. If you could share any info to point me along, that'd be great!
Thanks!
M Ridzon
Sure can.
profile('on');
HW_5;
profile('off');
profile('viewer');
You'll get a nice-looking HTML report. Lots of options you can dig into, but it starts with a list of all functions called, and their respective run-times. Click the "HW_5' hyperlink and it'll show you line-by-line (with highlighting) run-times.

Accedi per commentare.

Risposte (2)

Greg
Greg il 12 Dic 2017
Modificato: Greg il 12 Dic 2017
You won't gain anything with
temp = x;
x = temp + 1;
over
x = x + 1;
If you have a lot of loops and the code takes a long time to run, you may be having pre-allocation issues. We'd need to see your code to help with that. Otherwise, just do a MATLAB answers search for pre-allocation.
You can think of it simply that the copy of x to add 1 to itself costs you the same as copying x into temp, to add 1 to that. Either way, you still end up with 2 variables in memory (temporarily or permanently), and one copy of x happening.

10 Commenti

Thanks for the feedback. I had a hunch my hypothesis was wrong, but I thought I'd ask. I attached my script here. Please go easy. Haha! Attaching it, I fear it may open Pandora's Box and folks would find a thousand other things wrong with my elementary coding expertise. I don't want to run down 20 rabbit holes as a result, but instead just find the latency. Haha! Hopefully, Greg can share some insight about running it in the Profiler.
Thanks,
M Ridzon
I have one guiding principle: teach as much as the student is willing to learn. If you care to get better, I'll care to help you (conversely, if you don't care, that's cool but I won't waste my time). If you'd like, I see your profile lets me contact you through email, I can (as I have time) give some pointers after looking through your attached code.
Greg,
Thanks for the offer. If you're willing, I'll take any feedback you can offer.
Here's been my experience on this Matlab forum: Folks on here are brilliant. But contrary to other forums I visit, I have found folks (many but not all) on here are very abbreviated and pinpointed about the help they'll offer. I ask a question. They give a very brief, but perhaps accurate response, and then drop it like a bad habit to move onto the next thing. So if I have any follow-on questions to the same topic, it's very unlikely I'll get any help. So looking at this scenario through that lens, is why I made my previous comments. I don't feel folks on here are out to "teach" so I try to succinctly ask my question without overwhelming them and being a pest, since it's likely they already moved on to the next thing anyways. I wish more folks on here would "teach" as you say.
That said, again if you're willing to offer any insight, please do so. The student is willing to learn.
Thanks,
M Ridzon
@Matthew: Your observation is spot on. But here's a little background to put it in context. We (the common answerers) have all been burned by a questioner that just didn't care. After the first time you spend an hour crafting an amazing response (see Walter Roberson's answer here) and the person asking the question declines it, or deletes the question entirely, you stop putting in the effort up front.
Also, the activity feed is relatively new, and boy is it handy for staying connected to previous efforts. Hopefully some of us start reducing our "drop it and move on" behavior.
Matthew, overall the code is not bad. It has two things I like to see: (1) fairly well commented code (though it never hurts to add more), and (2) descriptive variable names. I insist on those two things when people write code for me. No one likes to look at a long, uncommented alphabet soup of code. It's impossible to follow, maintain, understand, or inherit from someone. One suggestion I'd have, though it's a matter of style, is to use more spaces around things, like after commas and around relational and mathematical operations (+ - <=, etc.)
In response to Matthew, upset that some won't respond to a followup.
I would point out that many times, someone asks a question about problem X. Having gotten help on that point, they then ask a second, unrelated question in the comments. If they got an answer to that question, they would then continue asking unrelated followup questions, making one question into their own personal consulting forum.
Note that those who do actively answer questions may answer dozens or even hundreds of questions in a week. Each such answer may take a few minutes to write, and for some of us, well over an hour, or more. If an answer was complete as posted, then once one sees needy followups start to appear, it becomes time to stop responding.
I could see this being a problem for many questioners. That is not me though. I've committed that offense a few times and been reproved for it, thus learning my lesson to not run down rabbit holes. But often my follow-up questions are completely related to the subject matter at hand in the thread. As I said though, they are usually abandoned unfortunately.
Matthew, I went back over all of your Questions. In most of them, the volunteers are the last contributors to the question, so if you wanted more response then you should have said something. Most of the Questions in which you were the last contributor were cases in which you were thanking people for having solved the problem. We have to go back to November 2015 to find something that wasn't solved for you, when you asked about Blauhus conditions. We have to go back to November 2014 to find a case where you had asked a follow-up that was not replied to.
Thanks for your willingness to check into this, but your conclusion is incorrect. I don't have time to dig through all of my old Matlab Forum emails. But I strolled back just a few months (because I remember an incident recently) and found this one unresolved: https://www.mathworks.com/matlabcentral/answers/358919-ode45-to-solve-system-of-odes?s_tid=email_ans_new_ans_ans_h#answer_283619
My apologies, I didn't set out to open Pandora's Box on this forum. I merely replied to Greg's point to say that inquirers sometimes get left abandoned with unanswered questions on this forum. Some folks on here will provide an answer and quickly drop this issue like a bad habit to move onto the next thing, leaving the inquirer hanging. Clearly some contributors are not like that, but some are. We could go back-n-forth in a spitting contest about this, but none of us have the time for that or find it productive. I suppose the least we could hope for out of this discourse though, is that those contributors who operate in this poor manner, will rethink their ways. Who knows though?!
Regards,
M Ridzon
We cannot tell from the timestamps at this point whether you or James last posted to that thread.
If it was you, then we can see that your question there was vague: just saying that you did not understand some of Jame's answer does not give us any hint about what aspects you did not understand.
I know that when I am answering questions, if someone posts and just says that my answer wasn't understandable and asks for other people to answer, without giving any information about what more is desired, then I might well not follow up, as such responses essentially tell me that my input on the topic is no longer welcome. And as someone looking in on a thread that someone else has been responding to, it is typically too frustrating to go through and break down the problem a completely different way when I have no idea what it was that was not understood in what the other person said; there is entirely too much chance that I would be wasting my time.
The volunteers thrive on specific questions, not on "I don't understand you; someone else please answer".

Accedi per commentare.

x = x + 1
"So I assume x has to first be moved to RAM to then complete the calculation and subsequently store the new value."
Unless you are using the fairly new "tall arrays", x will already be in RAM. The parser will look up x in the symbol table (if it does not already know where it is), and what type it is, and locate the plus() function applicable for the data type, and will call the appropriate plus(x,1) . In the case of double() that is a built-in routine.
The built-in routine will check the sizes and see that you are adding a scalar to an array. It will check the size of the array to decide how best to handle it. If the array is not big, it will call into an internal routine. The internal routine will construct an output variable of appropriate size, and will then do a straight-forward loop through doing the addition. For double() the addition will just involve using the built-in machine instructions to add values. The internal routine will return the constructed temporary output variable, and that will be returned from plus. The parser will then see that the output is to be assigned to x, and will decrease the usage count associated with the storage x has been using. If the usage count reaches 0, then the parser will return that old memory. The parser will then adjust symbol table associated with x to point to the new location and size (the parser does not assume that the '+' operation returns something the same size as the original.)
I said above, "if the array is not big". If it is sufficiently large, then the built in plus routine will call into a high performance multi-threaded library to do the addition of 1. Even though it might hardly seem worth the effort of calling a high-performance library, the fact that the addition of 1 will be done in parallel over sections of memory can speed things up, and also the high performance libraries know all the tricks about cache sizes and all the tricks about SIMD (Single Instruction Multiple Data) to be able to trigger several additions with the same instruction. But there is overhead to going through all that trouble, which is why the library is only invoked if the array is large enough.
There are some performance tricks. There is a special case for functions of the form
function variable = function_name(variable, ...)
where the same variable is passed in and is written to, and the user calls the function writing to the same variable that the user passes in, and the variable has not been copied since it was last assigned to. In that particular case, MATLAB knows that it can re-use the memory -- so a function call
x = plus1(x)
function x = plus1(x)
x = x + 1;
has the potential for higher performance than a plain x = x + 1 . It is an obscure case.

3 Commenti

Walter,
Thank you very much for the detailed answer. That was helpful.
Thanks,
M Ridzon
Note that one additional requirement for the inplace operation of x=x+1 is that this line
x = plus1(x)
needs to be inside of a function itself.
For kicks, I tried gathering actual execution time metrics on these 2 cases. I'm not sure if I'm implementing the "obscure case" correctly, but my results are negligible.
I tried with x = 1:1e9 (nearly an 8GB variable in RAM), and with passing that into the parent function as well as declaring it inside before calling plus1(x). The largest execution time delta I saw between using the plus1 function and not was 10%, and that was actually with plus1 being slower.

Accedi per commentare.

Richiesto:

il 12 Dic 2017

Commentato:

il 17 Dic 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by