Reputation: 71
I have got this doubt many times, but didn't figure it out the correct soltion. This time I want clear it off. I have situation like
1.
String sNumber="ksadfl.jksadlf";
if(sNumber.lastIndexOf('.')>0)
//do something
...
...
if(sNumber.lastIndexOf('.')>1)
//do something
...
2.
int index = sNumber.lastIndexOf('.');
if(index>0)
//do something
...
...
if(index>1)
//do something
...
what are the trade offs between first way and second way? which one is better in storing the result in a variable or calling the function two times?
Upvotes: 6
Views: 1427
Reputation: 718678
In this example, the 2nd form is better (in most plausible situations1) from a performance perspective, and (IMO) more readable.
In general, there are a couple of trade-offs to consider:
Also, you also need to consider the cases where the method may have side-effects, and whether it may give a different answer on two successive calls. In those cases, calling the method twice is semantically different to calling it once and storing the result in a temporary variable.
1 - The index
variable makes the stack frame 1 word larger. Normally this doesn't matter, but if the code is in a recursive method that gets called in a deeply recursive fashion, that 1 extra word multiplied by a number of nested calls could result in a StackOverflowError
.
Upvotes: 5
Reputation: 2558
Your question is a classic trade-off between memory usage and CPU usage.
First you have to determine if you want your program to have a small footprint in memory or whether you want it to run fast. Solution 1 will most likely result in the least memory usage (since you are not storing an int), while solution 2 will result in the fastest execution time since you don't have to do the indexOf computation twice.
That being said storing calculations in memory does not always result in the fastest program. Having a small memory footprint often result in fewer cache misses. Cache misses is very often much slower than simply doing the calculation twice.
To get an idea of the importance of minimizing cache misses take a look at this post about the performance of list vs. vector in C++. One would expect the list to be much faster than the vector since data doesn't have to be copied around. The list implementation does however have much more random access of data, and therefore a lot more cache misses resulting in vector being alot faster.
In the end I would suggest that you measure the performance of the two solutions and determine which combination of memory and cpu usage you like the most.
Upvotes: 1
Reputation: 22705
Since the question only talks about 2 lean lastIndexOf
calls, there's actually little difference between the two. If it's a remote call via a proxy or something that does a database hit, then storing the method result into a variable would see you some improvement.
The good thing about calling the method again is that you'll always get the latest value of the index, in case it was manipulated somewhere in the earlier blocks. On most cases, #2 will be the right choice, but for your exact example above, it's #1.
Upvotes: 3
Reputation: 6525
Your doubts are obvious. Your 2nd approach is good and efficient.
Because if, your string variable data is not changing every time then no need to get the index every time. No, need to use sNumber.lastIndexOf('.')
again and again. Every time when your lastIndexOf()
function will run , it will take extra time and other resources.
So, its better you use 2nd option which makes your program faster and shorter.
Upvotes: 2