In C #, the efficiency of generic basic types is nearly doubled.

Source: Internet
Author: User

In C #, the efficiency of generic basic types is nearly doubled.
?? In C #, the efficiency of generic basic types is nearly doubled.


The test result is as follows:

CSharp class and generic TotalMilliseconds: 270772.9229
CSharp generic TotalMilliseconds: 269963.3999
CSharp normal TotalMilliseconds: 159716.9094


Test code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static class InsertionSort
 
   where T : IComparable        {            public static void Sort(T[] a)            {                for (var i = 0 + 1; i <= a.Length - 1; i++)                {                    var tmp = a[i];                    int j = i;                    while (j > 0 && a[j - 1].CompareTo(tmp) > 0)                        a[j] = a[--j];                    a[j] = tmp;                }            }        }        public static void CSharpInsertionSortGeneric
  
   (T[] a) where T : IComparable        {            for (var i = 0 + 1; i <= a.Length - 1; i++)            {                var tmp = a[i];                int j = i;                while (j > 0 && a[j - 1].CompareTo(tmp) > 0)                    a[j] = a[--j];                a[j] = tmp;            }        }        public static void CSharpInsertionSort(int[] a)        {            for (var i = 0 + 1; i <= a.Length - 1; i++)            {                var tmp = a[i];                int j = i;                while (j > 0 && a[j - 1].CompareTo(tmp) > 0)                    a[j] = a[--j];                a[j] = tmp;            }        }        static void Main(string[] args)        {            DateTime start = DateTime.Now;            int[] a = new int[200000];            for (int i = 0; i < 200000; i++)            {                a[i] = 100 - i;            }            InsertionSort
   
    .Sort(a);            DateTime end = DateTime.Now;            Console.WriteLine(string.Format("CSharp class and generic TotalMilliseconds: {0}", (end - start).TotalMilliseconds));            int[] a2 = new int[200000];            for (int i = 0; i < 200000; i++)            {                a2[i] = 100 - i;            }            CSharpInsertionSortGeneric(a2);            DateTime end2 = DateTime.Now;            Console.WriteLine(string.Format("CSharp generic TotalMilliseconds: {0}", (end2 - end).TotalMilliseconds));            int[] a3 = new int[200000];            for (int i = 0; i < 200000; i++)            {                a3[i] = 100 - i;            }            CSharpInsertionSort(a3);            DateTime end3 = DateTime.Now;            Console.WriteLine(string.Format("CSharp normal TotalMilliseconds: {0}", (end3 - end2).TotalMilliseconds));            //Console.WriteLine(String.Join(" ", a));            Console.ReadKey();        }    }}
   
  
 


Obviously, genericReduced efficiencyBut it increases flexibility.

But from the MSDN https://msdn.microsoft.com/en-us/library/ms172192.aspx, there is an advantage of generics:

Better performance.Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types.

Good performance?I suspect that this is not always a good performance in every situation! Why? It may be that the generic type will perform more memory operations during the runtime, thus reducing the efficiency.


Related Article

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.