C#中使用泛型对比使用通用基础类型效率降低近一倍
下面是测试结果:
CSharp class and generic TotalMilliseconds: 270772.9229
CSharp generic TotalMilliseconds: 269963.3999
CSharp normal TotalMilliseconds: 159716.9094
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static class InsertionSort<T> 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;
}