So i have an Onclick listener that's listening to about 20 ids (view.getId() >> switch R.id....). Does me not putting break statements at each case affect performance?
thanks for your help.
So i have an Onclick listener that's listening to about 20 ids (view.getId() >> switch R.id....). Does me not putting break statements at each case affect performance?
thanks for your help.
Not putting break
would decrease performance if anything, as instead of quitting the switch
block, your app would have to go through all other case
statements.
I know you've already answered your question, but I'll just post this comment in case anyone is particularly interested in the answer to your title. In general switch statements are highly efficient. You can find a lot of details online regarding the performance comparisons of different java operations, but for large comparison sets, switch statements allow for direct indexing in the best case, through to O(log2(n)), where as if statements are on average n/2 and n on worst case.
Additionally, switch statements are much more maintainable than a long set of if-else statements.
So in general: If you are comparing integers and have more than 2 comparisons, it's usually best to use a switch statement, otherwise use an if-else.
For more details: What is the relative performance difference of if/else versus switch statement in Java?