Comparison of If statement and switch running performance

Source: Internet
Author: User

When I first started writing a program, I liked to write if (){...} else if (){...} else {...}, as a result, my program seems to be an if statement. Some people once mentioned it to me, but I did not agree with it. I thought about how to implement it. Why should the program be so fancy and start to despise others, but .. When I tried to use a switch, I found that I made a big mistake. The program is not only accurate, but also efficient!

Next I will compare the two different process control statements:

First, let's talk about the if statement. The if statement is the earliest and the first process control statement that everyone may be familiar with, because it is more in line with our homeopathic thinking logic: Let's take a very simple example:
If (A> B)
{A = B ;}
Else
{B = ;}

This is a very simple and classic if statement. If you put this example in the switch statement, it is not impossible to implement it, but it may be more troublesome to implement it, in a switch statement, a constant is required in case, and logical judgment is not allowed. Therefore, if statements are superior to switch statements! However, if you are judging the data of several constants, we recommend that you use the switch statement as follows:
Switch ()
{
Case: xxxx
/// Program code
Break;
Case: xxxxxx
/// Program code
Break;
}

Although such statements can also be implemented by the if statement, the performance is a little far from that of the switch statement. Everyone who writes programs has experience in database architecture, we usually need to consider indexes when constructing data with a large amount of data. Why should we consider indexes? As for this, I don't need to talk about it? I will write a separate article on Database indexes to discuss with you! In my opinion, the switch statement is directly positioned. In my opinion, there are many similarities with indexes. This is what Microsoft Works like! The switch statement directly located should be superior to the IF statement in terms of performance. Can you imagine it? Especially for the comparison of "string", the effect may be more obvious! For more in-depth discussions, I hope you can give me some additional information and let us all study and discuss it together...

Finally, we hope that our programmers will be good at exploring, discovering new knowledge, summing up and improving themselves!

Support originality ~~~

After I published this article, many people began to doubt it. I have found a good article on the Internet and reproduced it, I have also read this article in detail. It is indeed good to write and confirms that my above inference is correct:

Article: http://www.cnblogs.com/yeah/archive/2009/02/16/1392094.html

From: http://www.cnblogs.com/yeah/archive/2009/02/16/1392077.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.