If vs. Switch, test and analysis

Source: Internet
Author: User
Tags switch case

I remember a long time ago, a buddy in the blog Park complained that the source code of. Net was too bad and switch was everywhere. I did a test at that time, proving that the switch has a much higher performance than if. I saw this topic again today. Well, let's test it again.

Code:

Code
Using System;
Using System. Collections. Generic;
Using System. diagnostics;
Using System. text;

Namespace Leleapplication1
{
Class Program
{
Static Int32 count =   100000000 ;

Static Int32 testifelse (int32 value)
{
Int32 I =   0 ;


Stopwatch SW =   New Stopwatch ();
Sw. Start ();
For ( Int J =   0 ; J < Count; j ++ )
{
If (Value =   1 )
I + =   1 ;
Else   If (Value =   2 )
I + =   2 ;
Else   If (Value =   3 )
I + =   3 ;
Else   If (Value =   4 )
I + =   4 ;
Else   If (Value =   5 )
I + =   5 ;
}
Sw. Stop ();
Console. writeline ( " Testifelse: "   + Sw. elapsedmilliseconds );
Return I;
}

Static Int32 testswitch (int32 value)
{
Int32 I =   0 ;

Stopwatch SW =   New Stopwatch ();
Sw. Start ();

For ( Int J =   0 ; J < Count; j ++ )
{
Switch (Value)
{
Case   1 :
I + = 1 ;
Break ;
Case   2 :
I + = 2 ;
Break ;
Case   3 :
I + = 3 ;
Break ;
Case   4 :
I + = 4 ;
Break ;
Case   5 :
I + = 5 ;
Break ;
Default :
Break ;
}
}
Sw. Stop ();
Console. writeline ( " Testswitch: "   + Sw. elapsedmilliseconds );

Return I;
}

Static   Void Main ( String [] ARGs)
{
Testifelse (5);
Testswitch (5);
Console. Read ();
}
}
}

 

Release compilation. Test results:

Testifelse: 613
Testswitch: 165

Performance gap about 4 times. If we look at Il through decompilation, we will find that the testswitch method has the following sentence:

Switch (l_0032, l_0038, l_003e, l_0044, l_004a)

This statement implements a jump table.

As a front-line worker said, this switch command is an index jump, and if... else is an index-free jump. If... else is O (n) level, switch... case is O (1) level.

If we increase the number of branches of the above test code to 10, and test the performance of testswitch (10) and testifelse (10), we will find that the former is almost 7-8 times faster than the latter.

For more information, see a chapter in understanding computer systems. (If you forget which chapter is not around, the difference between switch and if is explained ). You can also refer to this articleArticle: Http://www.9php.com/faq/cxsjl/c/2008/10/1435098132356.html .. For more information, see: http://www.cnblogs.com/yeah/archive/2009/02/16/1392094.html.

If switch (string...) is tested, switch has the same performance as if... else. I thought it was impossible to generate a jump table. That is to say, in this case, switch case is still at the O (1) level branch statement.

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.