android - Non conditional boolean to int conversion java - micro optimisation -
this question has answer here:
i'm new java , i'm writing android game - , there's 1 line of code in tight loop that's annoying me.
targetblocksremain += letterarray[column][row].istargetblock() ? 1 : 0;
i feel must possible write without conditional statement , therefore without potential branch prediction performance hit.
i'll optimise out tight loop i'm not massively concerned in long run, it'd nice know if there's way resolve similar situations.
edit clarify - pseudo theoretical questions - there lots of other things improve this, such store istargetblock int. i'm not interested in other suggestions, wondering if there's way resolve particular issue.
edit 2 hoping @ dalvik vm doing shortly work out how handles this.
i know might not avoid conditional statement:
do once in activity:
hashmap<boolean, integer> map=new hashmap<boolean, integer>(); map.put(false, 0); map.put(true, 1);
and in loop
targetblocksremain += map.get(letterarray[column][row].istargetblock());
Comments
Post a Comment