Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

java - Which combination of switch and if/else is faster and why?

I have two sets of conditions. One has two possible values and the other has more (in this example I wrote 3 cases but there could be up to 8). Which code runs faster and is less error prompt (more accurate)?

code a)

if (letter.equals(a)) {
  switch (number) {

    case 1:
           .........
    case 2:
           .........
    case 3:
           .........
  }
} else if (letter.equals(b)) {
  switch (number) {

    case 1:
           .........
    case 2:
           .........
    case 3:
           .........
  }
}

code b)

switch (number) {

    case 1:

           if (letter.equals(a)) {
              .........
           } else if (letter.equals(b)) {
              .........
           }

    case 2:

           if (letter.equals(a)) {
              .........
           } else if (letter.equals(b)) {
              .........
           }

    case 3:

           if (letter.equals(a)) {
              .........
           } else if (letter.equals(b)) {
              .........
           }
  }

Please tell me if you think there is a better option other than these two. (I could also create a parameter that gets both letter and number and create 6 cases using that.)

Thank you in advance!

question from:https://stackoverflow.com/questions/65870983/which-combination-of-switch-and-if-else-is-faster-and-why

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Which code runs faster and is less error prompt (more accurate)?

Answer: In this specific case performance is not the issue. Because no matter how you are going to use this actual execution numbers are same. But you can improve the code readability and making it less errorprone.

Instead of worrying about performance, start with SOLID principle. Why don't you just break this big method into some smaller method which has a concrete responsibility. It will make code more beautiful and less error prone. For example:

Methods:

void processA(int number){
switch (number) {

    case 1:
           .........
    case 2:
           .........
    case 3:
           .........
  }
}

void processB(int number){
    
  switch (number) {

    case 1:
           .........
    case 2:
           .........
    case 3:
           .........
  }
}

///
now from the main method you could simply call:


if (letter.equals(a)) {
   // call the method which will process A
   processA(number);
} else if (letter.equals(b)) {
   // call the method which will process A
   processB(number);
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[5] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...