U E D R , A S I H C RSS

Java/Mode Selection Performance Test

Servlet —„œ, ˜Š” Switch - Case “ งŽ€ „ธฐ ํ•˜Š” ฐฉฒ•— Œ€ํ•œ ””žธ & ํํฌŠค € —„œ˜ ตฌฒฝ.

1. if-else

Œ€ฐ• ‹คŒ˜ ฝ”“œ Šคํƒ€ฒƒด‹ค.
~cpp 
public class IfElse {
    public void printPerformance(String[] modeExecute) {
        long start;
        long end;
        start = System.currentTimeMillis();
        for (int i = 0; i < ModeChoicePerformanceTest.LOOPING_COUNT; i++) {
            executeIfElse(modeExecute);
        }
        end = System.currentTimeMillis();
        System.out.println("if - else elapsed time :" + (end - start) + "ms ");
    }

    private void executeIfElse(String[] modeExecute) {
        for (int i = 0; i < modeExecute.length; i++) {
            executeWithIfElse(modeExecute[i]);
        }
    }

    public void executeWithIfElse(String mode) {
        if (mode.equals("One")) {
            doOne(1);
        } else if (mode.equals("Two")) {
            doTwo(1);
        } else if (mode.equals("Three")) {
            doThree(1);
        } else if (mode.equals("Four")) {
            doFour(1);
        } else if (mode.equals("Five")) {
            doFive(1);
        } else {
            doDefault(1);
        }
    }


    public void doOne(int i) {
        i = 10;
    }

    public void doTwo(int i) {
        i = 10;
    }

    public void doThree(int i) {
        i = 10;
    }

    public void doFour(int i) {
        i = 10;
    }

    public void doFive(int i) {
        i = 10;
    }

    public void doDefault(int i) {
        i = 100;
    }
}
ด ฐฉฒ•€ ‹จ †„ƒœก  €žฅ  ฅด‹ค. ํ•˜€งŒ, ํ•œํŽธœกœ
Seminar:WhySwitchStatementsAreBadSmell— ฆฌ€ •Š„Œ? ฐ.. ทธ— Œ€ํ•œ ฐ˜ก œกœ “ค–ด˜คŠ”ฒƒด 'ดด mode „ธฐด€กœ •ž—„œ˜ Switch-Statement —„œ˜ ˜ˆ™€Š” ‹คฅธ ƒํ™ฉ •„‹ˆƒ. –ดฐจํ” „ธฐ ดํ›„—” ทธƒฅ ํ•ด‹ €„ด ‹คํ–‰˜ณ  ด‹ค.' ณ  งํ• ฒƒด‹ค. €Œ”. ชจฅดฒ ‹ค.
ํ•œํŽธœกœ Šปด€Š” ฒƒœกœŠ”, switch กœ „ธฐ ‚˜ˆŒ mode string › parameter ™€˜ ค‘ณตด žˆ„ ฒƒดŠ”  ด ณดธ‹ค. ทธฆฌณ  ํ•˜‚˜˜ mode € Š˜–ด‚ •Œงˆ‹ค ํ•ด‹ method € Š˜–ด‚˜ณ , mode string ด Š˜–ด‚˜ณ , if-else ตฌฌธด š ธธ–ด„‹คŠ”  ด žˆ‹ค. €ธˆ€ ฉ”†Œ“œกœ ถ”ถœ„ ํ•ด†“€ ƒํ™ฉด€งŒ, งŒ  € €„ด ฉ”†Œ“œกœ ถ”ถœด •ˆ˜–ดžˆ‹คฉด? ทธ‹จ ํ•œงˆ”” ฐ–— ํ• งด —†‹ค. (‹จ,  € …ฌธ„ •„Š” ‚ฌžŒ— ํ•œํ•ด„œ) GotoStatementConsideredHarmful.

‘ฒˆงธ - Method reflection

~cpp 

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class MethodFullReflection {
    public void printPerformance(String[] modeExecute) throws InvocationTargetException, IllegalAccessException {
        long start;
        long end;
        start = System.currentTimeMillis();
        for (int i = 0; i < ModeChoicePerformanceTest.LOOPING_COUNT; i++) {
            executeReflection(modeExecute);
        }
        end = System.currentTimeMillis();
        System.out.println("elapsed time :" + (end - start) + "ms ");

    }

    private void executeReflection(String[] modeExecute) throws InvocationTargetException, IllegalAccessException {
        for (int i = 0; i < modeExecute.length; i++) {
            Method method = null;
            try {
                method = this.getClass().getMethod("do" + modeExecute[i], new Class[]{int.class});
                method.invoke(this, new Object[]{new Integer(1)});
            } catch (NoSuchMethodException e) {
                this.doDefault(1);
            } catch (SecurityException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
        }
    }

    private void doDefault(int i) {
        i = 100;
    }

    public void doOne(int i) {
        i = 10;
    }

    public void doTwo(int i) {
        i = 10;
    }

    public void doThree(int i) {
        i = 10;
    }

    public void doFour(int i) {
        i = 10;
    }

    public void doFive(int i) {
        i = 10;
    }

}
žฅ  : MODE € ถ”€ •Œงˆ‹ค doXXX ‹œกœ ด„„  •ํ•ดณ  ด ‹คํ–‰ํ•˜ฉด œ‹ค. กฐ„ธฐ €„˜ ฝ”“œ€ ฆ€˜€ •Šณ , ํ•ด‹ Mode € ถ”€ •Œงˆ‹ค ฉ”†Œ“œ ํ•˜‚˜งŒ ถ”€ํ•ดฉด œ‹ค.
‹จ  : žฐ”—„œŠ” Method Reflection & Invoke € —„ฒญ Šฆฌ‹ค.; †„Š” ฐ‘˜ ฐธกฐ.

„ฒˆงธ. œ„˜ ฐฉฒ•„ ณด™„ํ•œ ฐฉฒ•ด‹ค. ฐ”กœ ข…˜ Table Lookup.

~cpp 

import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * User: Administrator Date: 2003. 7. 12. Time: ˜ค „ 12:48:38
 */
public class MethodTableLookupReflection {
    public void printPerformance(String[] modeExecute) throws InvocationTargetException, IllegalAccessException {
        long start;
        long end;
        start = System.currentTimeMillis();
        initReflectionMap(modeExecute);
        end = System.currentTimeMillis();
        System.out.println("reflection with method initialize table elapsed time :" + (end - start) + "ms ");

        start = System.currentTimeMillis();
        for (int i = 0; i < ModeChoicePerformanceTest.LOOPING_COUNT; i++) {
            executeReflectionWithMapping(modeExecute);
        }
        end = System.currentTimeMillis();
        System.out.println("reflection with method elapsed time :" + (end - start) + "ms ");

    }

    private static Map methodMap;


    private void executeReflectionWithMapping(String[] modeExecute) throws InvocationTargetException, IllegalAccessException {
        for (int i = 0; i < modeExecute.length; i++) {
            Method method = (Method) methodMap.get(modeExecute[i]);
            if (method != null)
                method.invoke(this, new Object[]{new Integer(1)});
            else
                doDefault(1);
        }
    }

    private void initReflectionMap(String[] methodNames) {
        methodMap = new HashMap();
        for (int i = 0; i < methodNames.length; i++) {
            try {
                methodMap.put(methodNames[i], this.getClass().getMethod("do" + methodNames[i], new Class[]{int.class}));
            } catch (NoSuchMethodException e) {
            } catch (SecurityException e) {
            }
        }

    }

    private void doDefault(int i) {
        i = 100;
    }

    public void doOne(int i) {
        i = 10;
    }

    public void doTwo(int i) {
        i = 10;
    }

    public void doThree(int i) {
        i = 10;
    }

    public void doFour(int i) {
        i = 10;
    }

    public void doFive(int i) {
        i = 10;
    }
}
ด ฐฉฒ•€ œ„˜ ฐฉฒ• ฐ™€ žฅ „ €‹ˆฉด„œ ํํฌŠค ˜ 10ฐฐ€Ÿ‰ ํ–ฅƒ‹œํ‚จ‹ค.

„ฒˆงธ. Inner Class — Œ€ํ•ด Command Pattern ˜ ‚ฌšฉ.

~cpp 

import java.util.Map;
import java.util.HashMap;

/**
 * User: Administrator Date: 2003. 7. 12. Time: ˜ค „ 12:57:7
 */
public class InterfaceTableLookup {
    protected Map modeMap = new HashMap();

    public void printPerformance(String[] modeExecute) {
        long start;
        long end;
        start = System.currentTimeMillis();
        initModeMap();
        end = System.currentTimeMillis();
        System.out.println("interface table lookup init table elapsed time :" + (end - start) + "ms ");

        start = System.currentTimeMillis();
        for (int i = 0; i < ModeChoicePerformanceTest.LOOPING_COUNT; i++) {
            executeInnerclassMapping(modeExecute);
        }
        end = System.currentTimeMillis();
        System.out.println("interface table lookup elapsed time :" + (end - start) + "ms ");


    }

    private void executeInnerclassMapping(String[] modeExecute) {
        for (int i = 0; i < modeExecute.length; i++) {
            executeMode(modeExecute[i]);
         }
    }

    private void executeMode(String s) {
        IMode mode = (IMode) modeMap.get(s);
        if (mode == null) doDefault(1);
        else mode.execute(1);
    }

    public class ExOne implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public  class ExTwo implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExThree implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExFour implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExFive implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    private void initModeMap() {
        modeMap.put("One", new ExOne());
        modeMap.put("Two", new ExTwo());
        modeMap.put("Three", new ExThree());
        modeMap.put("Four", new ExFour());
        modeMap.put("Five", new ExFive());
    }
}
ด ฐฉฒ•€ initModeMap —„œ งคฒˆ Mode— Œ€ํ•œ “ก„ ํ•ดค˜• ํ•œ‹ค. ํํฌŠคŠ” Method Reflection ณด‹ค ํ›จ”ฌ  ฅด‹ค.

งˆ€ง‰ ฐฉฒ• - interface & reflection

œ„˜ ฐฉฒ•— initModeMap „ reflection œกœ ฒ˜ฆฌํ•œ ฒƒด‹ค.
~cpp 
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Administrator Date: 2003. 7. 12. Time: ˜ค „ 1:2:16
 */
public class InterfaceTableLookupReflection {
    public void printPerformance(String[] modeExecute) throws NoSuchMethodException, InstantiationException, ClassNotFoundException, InvocationTargetException, IllegalAccessException {
        long start;
        long end;
        start = System.currentTimeMillis();
        initModeMapWithReflection(modeExecute);
        end = System.currentTimeMillis();
        System.out.println("interface reflection & table lookup init able elapsed time :" + (end - start) + "ms ");

        start = System.currentTimeMillis();
        for (int i = 0; i < ModeChoicePerformanceTest.LOOPING_COUNT; i++) {
            executeInnerclassMapping(modeExecute);
        }
        end = System.currentTimeMillis();
        System.out.println("interface reflection & table lookup elapsed time :" + (end - start) + "ms ");

    }

    private void executeInnerclassMapping(String[] modeExecute) {
        for (int i = 0; i < modeExecute.length; i++) {
            executeMode(modeExecute[i]);
         }
    }

    private void executeMode(String s) {
        IMode mode = (IMode) modeMap.get(s);
        if (mode == null) doDefault(1);
        else mode.execute(1);
    }

    protected Map modeMap = new HashMap();
    public final static String modeClassHeader = "Ex";
    String expectedClassNameHeader = this.getClass().getName() + "$" + modeClassHeader;

    private void initModeMapWithReflection(String[] modeExecute) throws InstantiationException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Class[] consParamClasses = new Class[]{this.getClass()};
        Object[] consParams = new Object[]{this};


        Class[] inners = this.getClass().getClasses();
        for (int i=0;i<inners.length;i++) {
            if (inners[i].getName().startsWith(expectedClassNameHeader)) {
                Constructor innerCons = inners[i].getDeclaredConstructor(consParamClasses);
                modeMap.put(modeExecute[i], innerCons.newInstance(consParams));
            }
        }
    }

    public void doDefault(int i) {
        i = 100;
    }

    public class ExOne implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public  class ExTwo implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExThree implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExFour implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }

    public class ExFive implements IMode {
        public void execute(int i) {
            i = 10;
        }
    }
}
ด ฐฉฒ•€ Mode ถ”€‹œ ทธƒฅ ExModeName ‹œกœ ถ”€ํ•ดฉด œ‹ค. ทธŸฌฉด„œ Mode กฐ„ธฐ €„ด €ํ•˜€ •ŠŠ”‹ค. Reflectionœกœ table lookup „šฐŠ” €„ด‚˜ Mode กฐ„ธฐ €„„ •„— ƒœ„ ํดž˜Šคกœ ถ”ถœํ•  ˆ˜ žˆ‹ค. ํํฌŠคฉด—„œŠ” ˜™ธกœ •ž—„œ ˆ˜™œกœ map „ „šฐŠ” ฐฉฒ• ฐ™‹ค. œ —ฐ„ ํํฌŠค ‘€€€   ˆํžˆ –ดšธ–ด€Š” ฐฉฒ•ด‹ค.


ดœ„˜ ํ…ŒŠคํŠธ“ค„ ํ•œฒˆ— ‹คํ–‰‹œํ‚คธฐ œ„ํ•œ runner class.
~cpp 
import java.lang.reflect.InvocationTargetException;

/*
  ํ‰€ํ•˜ คŠ” กฐ“ค :
  1. ํ•ด‹ method ˜ naming œกœ reflection call.
  2. switch - case กœ mode ตฌ„.
  3. interface & class - command pattern ˜ ตฌํ˜„.

  ํ‰€š”†Œ“ค
  1. ํ•ด‹ ฝ”“œ ž‘„Œ€„ Š˜–ด‚˜Š” ฐ˜ณต ž‘—…“ค
  2. performance
  3. maintance
*/
public class ModeChoicePerformanceTest {
    public static final int LOOPING_COUNT = 1000000;

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, InstantiationException {
        String[] modeExecute = new String[]{"One", "Two", "Three", "Four", "Five", "hugu"};

        new IfElse().printPerformance(modeExecute);
        new MethodFullReflection().printPerformance(modeExecute);
        new MethodTableLookupReflection().printPerformance(modeExecute);
        new InterfaceTableLookup().printPerformance(modeExecute);
        new InterfaceTableLookupReflection().printPerformance(modeExecute);
    }
}

ฒฐ

~cpp 
if - else elapsed time :611ms 
elapsed time :61889ms 
reflection with method initialize table elapsed time :0ms 
reflection with method elapsed time :6459ms 
interface table lookup init table elapsed time :10ms 
interface table lookup elapsed time :741ms 
interface reflection & table lookup init able elapsed time :10ms 
interface reflection & table lookup elapsed time :731ms 
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:33
Processing time 0.0187 sec