java - Should I minimize the number of "if" statements or "for" loops? -


i have list of objects need have multiple, conditionally applied, operations applied each element. more efficient take "if-for" approach or "for-if" approach. put way, should minimize number of if statements or number of for loops? there standard this?

what reliable way determine this?

"if-for" to approach minimize if statements

public void ifformethod() {     if (conditiona) {         (object o : listofobjects) {             doa(o);         }     }      if (conditionb) {         (object o : listofobjects) {             dob(o);         }     } } 

"for-if" approach minimize loops

public void forifmethod() {     (object o : listofobjects) {         if (conditiona) {             doa(o);         }         if (conditionb) {             dob(o);         }      } } 

assumptions

  • the conditions simple booleans , not change while iterating.
  • one or more conditions true. (there more 2 conditions)
  • each condition independent of other conditions.
  • the inner methods not conflict or interact each other @ all. order in executed irrelevant.

first, let's take @ complexity of methods you've shown far:

  • the ifformethod performs k checks, m of return true. each of these m, there iteration on n objects. complexity, then, k+nm.
  • the forifmethod iterates on n objects , performs k comparisons on each iteration. complexity, then, k+n(k-1)=nk.

in both cases, k conditions have evaluated @ least once, difference here in nm , n(k-1) addends. asymptotically, m fraction of k (you said m approximately .75k), these both o(nk), k+nm < k+n(k-1), ifformethod might faster forifmethod. difference in actual run time going depend on factors such actual time takes iterate on array, magnitude of k. you're going start getting issues such memory locality (both objects code).

here's approach might find interesting, though. ideally, you'd want iterate through list of objects once, , wouldn't want have check boolean conditions multiple times. abstract away actions you're performing in such way combine them single action (and you'd incorporate actions correspond conditions true), , perform compound action on each element in list. here's code this.

the idea there actions, , can construct action performs doa , action performs dob. based on conditions, can create compound action includes doa action if doa condition true, , dob action if dob condition true. iterate through objects, , call perform compound action on each object. asymptotically, k+nm method, in theory performs nicely, again, actual performance here depend on of tricky constants, , memory locality issues.

import java.util.arraylist; import java.util.list;  public class compoundactionexample {      /**      * action used argument.      */     interface action {         void act( object argument );     }      /**      * compound action action acts on argument      * passing argument other actions.      */     static class compoundaction implements action {         /**          * list of actions compound action perform.  additional          * actions can added using {@link #add(action)}, , list          * accessed through {@link #act(object)} method.          */         private final list<compoundactionexample.action> actions;          /**          * create compound action specified list of actions.          */         compoundaction( final list<compoundactionexample.action> actions ) {             this.actions = actions;         }          /**          * create compound action fresh list of actions.          */         compoundaction() {              this( new arraylist<compoundactionexample.action>() );         }          /**          * add action compound action.          */         public void add( compoundactionexample.action action ) {             actions.add( action );         }          /**          * act on argument passing argument each of           * compound action's actions.          */         public void act( final object argument) {             ( compoundactionexample.action action : actions ) {                 action.act( argument );             }         }     }      public static void main(string[] args) {         // conditions , list of objects         final boolean conditiona = true;         final boolean conditionb = false;         final object[] listofobjects = { "object1", "object2", "object3" };          // compound action encapsulates things want         final compoundaction compoundaction = new compoundaction();          // if conditiona true, add action compound action          // perform doa.  conditiona evaluated once.         if ( conditiona ) {             compoundaction.add( new action() {                 public void act( final object argument) {                     system.out.println( "doa("+argument+")" ); // doa( argument );                 }             });         }          // if conditionb true, add action compound action         // perform dob. conditionb evaluted once.         if ( conditionb )  {             compoundaction.add( new action() {                 public void act(object argument) {                     system.out.println( "dob("+argument+")" ); // dob( argument );                 }             });         }          // each object, apply compound action         ( final object o : listofobjects ) {             compoundaction.act( o );         }     } } 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -