1

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.

Cody
  • 8,686
  • 18
  • 71
  • 126
  • 1
    Just as a heads up, you can always save some space by writing totalFloat+=someFloatVar; – Max Jan 17 '12 at 15:39
  • What you have is just fine. If you are worried about potential overflow then use BigDecimal's. – Perception Jan 17 '12 at 15:40
  • i am not sure i understand your question correctly... more efficient in regard to what? sum speed? – STT LCU Jan 17 '12 at 15:41
  • Efficient way to add numbers - now that's a new one. – JonH Jan 17 '12 at 15:41
  • is there a chance of overflow??? – Rajesh Pantula Jan 17 '12 at 15:41
  • What's the actual scenario? I can't think of any use for just continually increasing a variable by some amount until the user decides they've had enough. Also, more than one line of code and some extra details (such as if the value of `someFloatVar` is constant) would be great! – Anthony Grist Jan 17 '12 at 15:41
  • If you are talking about **Bitwise** operations, [there's no such thing as Bitwise operations on Floating point numbers][1]. [1]: http://stackoverflow.com/questions/1723575/how-to-perform-bitwise-operation-on-floating-point-numbers – bchetty Jan 17 '12 at 15:53
  • @AnthonyGrist the value of someFloatVar is not always constant. It's a number that gets updated periodically, and must be added to the total. – Cody Jan 17 '12 at 16:02

6 Answers6

1

You can remove one of the totalFloat by using the following syntax:

totalFloat += someFloatVar;

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 someFloatVar with a number depending on that time rather than adding it several times.

Jave
  • 31,598
  • 14
  • 77
  • 90
  • 2
    Isn't that the same thing, only a shorter way to write it? – Cody Jan 17 '12 at 15:39
  • @DoctorOreo - yes it is the same way - what exactly do you mean more efficient way. You are adding numbers - there really isn't a more efficient way. – JonH Jan 17 '12 at 15:40
  • @DoctorOreo I do agree, that is the best you can do. I really do not understand your question – Eugene Jan 17 '12 at 15:41
1

Make sure your values are primitives. There really is no better way to do it than what you have. Addition is native to all microprocessors and does not get any faster than that. If you do some calculations on your values then you will just be expending a lot more CPU.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
1

Short answer yes that is the most efficient way to add two floating point numbers. Long answer yes, but it shouldn't be a problem. That represents a single instruction in machine code so there is no other way to make it any faster. I believe most Android processors have FPU which means its really really fast. Technically you might be able to do some fixed point math, but processors, even on mobile, are so ridiculously fast going through the trouble to use fixed point math is way way over engineering.

http://developer.android.com/guide/practices/design/performance.html

In the end you should write the code as it makes the most sense, measure how fast it runs, if that isn't fast enough then use a profiler to determine where the code is spending the most time and fix that area with a better algorithm, caching, less frequent calls, etc. Remember the old saying "Make it work, make it right, make it fast. And repeat."

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
1

There's gotta be a more efficient way, right?

No there isn't a more efficient way to add number to a variable. In fact, this probably 2 or 3 machine instructions ... just a few nanoseconds.

However ...

These numbers are continually added up until the user decides to stop the task.

A better idea is to measure the duration between the start and stop events, and when the stop event occurs increment the variable by a value that is proportional to that duration.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • There is an interval of time in between these two numbers being added, it's not simply in a while loop. It's a calculation that is called with each update in location. (Every 90feet or 15 seconds). – Cody Jan 17 '12 at 15:50
  • So why are you bothered about the *efficiency* of an assignment statement that gets executed once every 15 seconds or so?? – Stephen C Jan 18 '12 at 02:59
  • I wasn't sure if as the numbers got bigger it caused a bigger performance hit, but I guess adding two numbers isn't that big of a deal. – Cody Jan 18 '12 at 04:46
0

The way you have it is fine (although you could write totalFloat += someFloatVar instead).

The only time I'd worry about a situation like this is if you're using a String. Strings are the only time you probably don't want to continuously add data to (but using a StringBuilder and its .append() method would be efficient, but this part is all off topic :)).

Alternatively, give the user a cap! Once the number gets way too large for your liking, stop the process yourself. Another possibility is NOT to use something with the syntax of something like while(true). Have the number update only every so often.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
0

I'm not entirely sure what you're asking.

I'm guessing that what you've got is something like this:

float someFloatVar = 0.5;
float totalFloat = 0.0;
while(true){
    totalFloat += someFloatVar;
}

Given that someFloatVar is static and DOESN'T change every time you add it you COULD do it this way (although the efficiency saving is negligible really). Record the timestamp you START the task and when the task is stopped minus that timestamp from the current timestamp. Decide on a rate of addition (possibly 100 a second or similar) and then multiply someFloatVar by rateOfAddition*timeDiffSecs and add that to the original value (totalFloat).

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224