0

I am doing some performance optimization for my java application and I am confuse about using the tmp variable to remove the method invocation in loop termination. Here is my situation:

Vector myVector = new Vector();
// some code
for (int i=0;i<myVector.size();i++){
//some code here;
}

I want to use

int tmp = myVector.size();
for(int i=0;i<tmp;i++){
//some code here
}

What would be negative impact of using second scenario ? My application is pretty large and I am not sure when and where myVector is being updated.

Ashish
  • 14,295
  • 21
  • 82
  • 127
  • 2
    http://howtodoinjava.com/2013/03/26/performance-comparison-of-different-for-loops-in-java/ – geoand May 02 '14 at 17:57
  • thank you for your suggestion I will go through the tutorial – Ashish May 02 '14 at 17:59
  • 6
    First rule of optimization: Don't do it _until you've profiled your application to see where the bottlenecks are_ – awksp May 02 '14 at 17:59
  • Also, look at [How to write a correct micro-benchmark in Java](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java). Benchmarking is not that easy, and the linked website could do a lot better with its benchmark. – awksp May 02 '14 at 18:02
  • @user3580294 thank you for your suggestion. I will go through that. – Ashish May 02 '14 at 18:04
  • 1
    First rule of optimization: *Don't Optimize*. Second rule of optimization (for experts only!): *Don't Optimize **yet***. – heptadecagram May 02 '14 at 18:27
  • @heptadecagram thank you for your advice but I don't see why the question get downvoted, isn't it a valid question – Ashish May 02 '14 at 18:44

1 Answers1

1

This change will not have any noticable impact on performance, neither positive nor negative. So you should not change this as long as there is no profound reason to do so.

Regarding your question

What could be negative impact of using second scenario ?

you should be aware that both implementations may behave differently in a multi-threaded environment. In the first case, changes of the vector that may be done by any other thread will be taken into account, and may affect how many times to loop is run. In the second case, the number of runs for the loop is computed once, and will not change later (even if the size of the vector changes). However, changing the contents of a vector while iterating over it with any of the both loops is dangerous and should be avoided if possible

BTW: The benchmark that was linked in the comment from @geoand is as flawed as a microbenchmark can be. This does not tell you anything.

Marco13
  • 53,703
  • 9
  • 80
  • 159
  • Thank you very much @Marco13. A very nice and clear explanation. I just do not know why the question got down voted. – Ashish May 02 '14 at 20:34
  • @Ashish There are similar questions ( http://stackoverflow.com/questions/2856835/is-arraylist-size-method-cached , http://stackoverflow.com/questions/1208320/what-is-the-cost-of-calling-array-length ...), maybe someone should just have marked it as "possible duplicate". However, the question whether it will have a "negative impact" can also refer to the possible different *behavior* that I mentioned, so ... It will be a +/-0 now ;-) – Marco13 May 02 '14 at 23:15