libgdx - Game optimisation in java concerning ArrayList<Object> elements casting to another variable inside for loop -
is usage of elements of arraylist:
for(int i=0; i<array_list.size(); i++){ object obj = array_list.get(i); //do **lots** of stuff **obj** }
faster one:
for(int i=0; i<array_list.size(); i++){ //do **lots** of stuff **array_list.get(i)**; }
it depends on how many times array_list.get(i)
called in sec code. if called once, there no difference between both methods.
if it's invoked multiple times, saving value in variable may more efficient (it depends on compiler , jit optimizations).
sample scenario first method may more efficient, compiled using oracle jdk's javac
compiler, assuming list contains string
objects:
for(int i=0; i<array_list.size(); i++){ string obj = array_list.get(i); system.out.println(obj); if(!obj.isempty()) { string o = obj.substring(1); system.out.println(o + obj); } }
in case, obj
saved local variable , loaded whenever used.
for(int i=0; i<array_list.size(); i++){ system.out.println(array_list.get(i)); if(!array_list.get(i).isempty()) { string o = array_list.get(i).substring(1); system.out.println(o + array_list.get(i)); } }
in case, multiple invokation list.get
observed in bytecode.
java libgdx
No comments:
Post a Comment