Will using 'var' affect performance?

后端 未结 12 1605
南方客
南方客 2020-11-22 14:01

Earlier I asked a question about why I see so many examples use the varkeyword and got the answer that while it is only necessary for anonymous types, that it is used noneth

12条回答
  •  情深已故
    2020-11-22 14:32

    It's depends of situation, if you try use, this code bellow.

    The expression is converted to "OBJECT" and decrease so much the performance, but it's a isolated problem.

    CODE:

    public class Fruta
    {
        dynamic _instance;
    
        public Fruta(dynamic obj)
        {
            _instance = obj;
        }
    
        public dynamic GetInstance()
        {
            return _instance;
        }
    }
    
    public class Manga
    {
        public int MyProperty { get; set; }
        public int MyProperty1 { get; set; }
        public int MyProperty2 { get; set; }
        public int MyProperty3 { get; set; }
    }
    
    public class Pera
    {
        public int MyProperty { get; set; }
        public int MyProperty1 { get; set; }
        public int MyProperty2 { get; set; }
    }
    
    public class Executa
    {
        public string Exec(int count, int value)
        {
            int x = 0;
            Random random = new Random();
            Stopwatch time = new Stopwatch();
            time.Start();
    
            while (x < count)
            {
                if (value == 0)
                {
                    var obj = new Pera();
                }
                else if (value == 1)
                {
                    Pera obj = new Pera();
                }
                else if (value == 2)
                {
                    var obj = new Banana();
                }
                else if (value == 3)
                {
                    var obj = (0 == random.Next(0, 1) ? new Fruta(new Manga()).GetInstance() : new Fruta(new Pera()).GetInstance());
                }
                else
                {
                    Banana obj = new Banana();
                }
    
                x++;
            }
    
            time.Stop();
            return time.Elapsed.ToString();
        }
    
        public void ExecManga()
        {
            var obj = new Fruta(new Manga()).GetInstance();
            Manga obj2 = obj;
        }
    
        public void ExecPera()
        {
            var obj = new Fruta(new Pera()).GetInstance();
            Pera obj2 = obj;
        }
    }
    

    Above results with ILSPY.

    public string Exec(int count, int value)
    {
        int x = 0;
        Random random = new Random();
        Stopwatch time = new Stopwatch();
        time.Start();
    
        for (; x < count; x++)
        {
            switch (value)
            {
                case 0:
                    {
                        Pera obj5 = new Pera();
                        break;
                    }
                case 1:
                    {
                        Pera obj4 = new Pera();
                        break;
                    }
                case 2:
                    {
                        Banana obj3 = default(Banana);
                        break;
                    }
                case 3:
                    {
                        object obj2 = (random.Next(0, 1) == 0) ? new Fruta(new Manga()).GetInstance() : new Fruta(new Pera()).GetInstance();
                        break;
                    }
                default:
                    {
                        Banana obj = default(Banana);
                        break;
                    }
            }
        }
    time.Stop();
    return time.Elapsed.ToString();
    }
    

    If you wish execute this code use the code bellow, and get the difference of times.

            static void Main(string[] args)
        {
            Executa exec = new Executa();            
            int x = 0;
            int times = 4;
            int count = 100000000;
            int[] intanceType = new int[4] { 0, 1, 2, 3 };
    
            while(x < times)
            {                
                Parallel.For(0, intanceType.Length, (i) => {
                    Console.WriteLine($"Tentativa:{x} Tipo de Instancia: {intanceType[i]} Tempo Execução: {exec.Exec(count, intanceType[i])}");
                });
                x++;
            }
    
            Console.ReadLine();
        }
    

    Regards

提交回复
热议问题