performance - Java for loop optimization - Storing getters before the loop -


suppose have following code..

for(element elm : elements)    if(elm instanceof foobar)       session.getsomething().getlistofsomething().add((foobar)elm); 

would not better following?

list<foobar> list = session.getsomething().getlistofsomething();  for(element elm : elements)    if(elm instanceof foobar)       list.add((foobar)elm); 

assuming , listofsomething not change during execution of loop. thinking cut down on number of callstack push/pops due reduced method calls.

is type of optimization being nitpicky? don't think java compilers assume optimize way.

edit : unsimplified code exclude usage of addall

it better in cases. reason java pretty strict on calling conventions, , every iteration have make 4 virtual calls. there exceptions (i.e. inlining jit), exotic.

though might better use default function:

session.getsomething().getlistofsomething().addall(elements) 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -