What is the most efficient way to continually add two numbers together?
For instance, I have something like the following:
totalFloat = totalFloat + someFloatVar
There’s gotta be a more efficient way, right? These numbers are continually added up until the user decides to stop the task. Also, someFloatVar is different each time around.
I know that I could do totalFloat += someFloatVar, but I was under the impression that it was the same thing efficiency wise. What I’m looking for is something that will not decrease the overall speed and performance of the application.
If not, is this the best way to add two numbers (that can be potentially quite large?)
In this context, I’m continually updating a mileage. Therefore this could be 5.678 meters or 895.67 meters, or larger.
You can remove one of the
totalFloatby using the following syntax:But this is equivalent to what you have written in terms of program efficiency (the only gain is that you don’t have to write as much). I’d say this sounds like a case of premature optimization; addition of two numbers is so effective there is pretty much no point to optimizing it.
Edit: What you could do, is perhaps that every time you want to update any displayed number (if you display it) or when the task is stopped, you calculate the time that have passed since the task was started, and then multiply
someFloatVarwith a number depending on that time rather than adding it several times.