C. efficiency of “as” and “is” (original CSDN 2017-10-07 11:49:18)

Time:2020-1-18

The 11th long holiday is about to pass. This year, I didn’t go home. I was just watching the Chinese version of C language standard 5.0. I saw the is operator and as operator yesterday. These two symbols are also used in normal projects. I haven’t compared their efficiency. Take advantage of the free time of holidays, and look at the efficiency first.

Is common method:

if(obj is T)
{
    T value = (T) obj;
}

First, judge whether obj is of type T, and then convert it.

这里写图片描述

As common method:

T value = obj as T;
if(value !=null)
{

}

If obj is not of type T, value = null; if obj is of value = (T) obj.

Expression as type is the same as expression is type? (type) expression: (type) null but the expression variable is evaluated only once.

这里写图片描述这里写图片描述

Test example:

class TestClass
{
    
}

class Program
{
    static Stopwatch sw_Timer = new Stopwatch();
    const int NUM = 100000;
    static int? TestIntType;
    static TestClass testClass = new TestClass();
    
    static void Main()
    {
        Console. Writeline ("value type test.");
        sw_Timer.Restart();
        for (int i = 0; i < NUM; i++)
        {
            object obj = i + 1;
            if (obj is int)
            {
                TestIntType = (int?)obj1;
            }
        }
        sw_Timer.Stop();
        Console. Writeline ("time required for is operation {0} times, {1} ticks.", num, sw_timer. Elapsedticks);
        
        sw_Timer.Restart();
        for (int i = 0; i < NUM; i++)
        {
            object obj = i + 1;
            TestIntType = obj as int?;
            if (TestIntType != null)
            {
                
            }
        }
        sw_Timer.Stop();
        Console. Writeline ("time required for {0} times of as operation, {1} ticks.", num, sw_timer. Elapsedticks);
        
        Console. Writeline ("reference type test.");
        sw_Timer.Restart();
        for (int i = 0; i < NUM; i++)
        {
            object obj = testClass;
            if (obj is TestClass)
            {
                TestClass objTest = (TestClass)obj;
            }
        }
        sw_Timer.Stop();
        Console. Writeline ("time required for is operation {0} times, {1} ticks.", num, sw_timer. Elapsedticks);
        
        sw_Timer.Restart();
        for (int i = 0; i < NUM; i++)
        {
            object obj = testClass;
            TestClass objTest = obj as TestClass;
            if (objTest != null)
            {
                
            }
        }
        sw_Timer.Stop();
        Console. Writeline ("time required for {0} times of as operation, {1} ticks.", num, sw_timer. Elapsedticks);
        
        Console.ReadKey();
    }
}

test result这里写图片描述

100000 tests, is > as for value type and is > is for reference type

Recommended Today

[reading notes] calculation advertising (Part 3)

By logm This article was originally published at https://segmentfault.com/u/logm/articles and is not allowed to be reproduced~ If the mathematical formula in the article cannot be displayed correctly, please refer to: Tips for displaying the mathematical formula correctly This article isComputing advertising (Second Edition)Reading notes. This part introduces the key technology of online advertising, which is […]