C#最快的2套排序值的联盟最快、联盟

2023-09-03 07:32:17 作者:超能力!

可能重复:   C#最快路口2台分拣数量

什么是最快的方式,以工会的2台分选的价值观呢?速度(大O)是很重要的位置;不清晰 - 假设这是正在做数百次

假设你不知道值的类型和范围,但有一个efficent 的IComparer< T> 和/或的IEqualityComparer< T>

由于数字下面一组:

  VAR LA =新INT [] {1,2,4,5,9};
变种岭=新INT [] {3,4,5,6,6,7,8};
 

我期待1,2,3,4,5,6,7,8,9,下面的存根可用于测试code:

 静态无效的主要(字串[] args)
{
    变种拉=新INT [] {1,2,4,5,9};
    变种岭=新INT [] {3,4,5,6,6,7,8};

    的foreach(在UnionSorted VAR项目(LA,RA,Int32Comparer.Default))
    {
        Console.Write({0},项目);
    }
    到Console.ReadLine();
}

类Int32Comparer:的IComparer<的Int32>
{
    公共静态只读Int32Comparer默认值=新Int32Comparer();
    公众诠释比较(INT X,int y)对
    {
        如果(X< Y)
            返回-1;
        否则,如果(X> Y)
            返回1;
        其他
            返回0;
    }
}

静态的IEnumerable< T> UnionSorted< T>(IEnumerable的< T> sortedLeft,IEnumerable的< T> sortedRight,IComparer的< T>比较器)
{
}
 

解决方案 数据结构和算法 三种简单的排序详解

这会让你的UnionSorted功能少一些通用的,但你可以通过有关类型的假设做一个小的改进。如果你这样做循环本身(而不是调用Int32Comparer)内的比较,然后说会节省一些函数调用的开销。

所以,你的UnionSorted声明成为本...

 静态的IEnumerable< INT> UnionSorted(IEnumerable的< INT> sortedLeft,IEnumerable的< INT> sortedRight)
 

然后你做到这一点的循环中,摆脱呼叫到 comparer.Compare() ...

  //无功补偿= comparer.Compare(左,右); // 太慢

INT补偿= 0;
如果(左<右)
    补偿= -1;
否则,如果(左>右)
    排版= 1;
 

在我的测试,这是快了大约15%。

Possible Duplicate: C# fastest intersection of 2 sets of sorted number

What is the fastest way to union 2 sets of sorted values? Speed (big-O) is important here; not clarity - assume this is being done millions of times.

Assume you do not know the type or range of the values, but have an efficent IComparer<T> and/or IEqualityComparer<T>.

Given the following set of numbers:

var la = new int[] { 1, 2, 4, 5, 9 };
var ra = new int[] { 3, 4, 5, 6, 6, 7, 8 };

I am expecting 1, 2, 3, 4, 5, 6, 7, 8, 9. The following stub may be used to test the code:

static void Main(string[] args)
{
    var la = new int[] { 1, 2, 4, 5, 9 };
    var ra = new int[] { 3, 4, 5, 6, 6, 7, 8 };

    foreach (var item in UnionSorted(la, ra, Int32Comparer.Default))
    {
        Console.Write("{0}, ", item);
    }
    Console.ReadLine();
}

class Int32Comparer : IComparer<Int32>
{
    public static readonly Int32Comparer Default = new Int32Comparer();
    public int Compare(int x, int y)
    {
        if (x < y)
            return -1;
        else if (x > y)
            return 1;
        else
            return 0;
    }
}

static IEnumerable<T> UnionSorted<T>(IEnumerable<T> sortedLeft, IEnumerable<T> sortedRight, IComparer<T> comparer)
{
}

解决方案

This will make your UnionSorted function a little less versatile, but you can make a small improvement by making an assumption about types. If you do the comparison inside the loop itself (rather than calling the Int32Comparer) then that'll save on some function call overhead.

So your UnionSorted declaration becomes this...

static IEnumerable<int> UnionSorted(IEnumerable<int> sortedLeft, IEnumerable<int> sortedRight)

And then you do this inside the loop, getting rid of the call to comparer.Compare()...

//var comp = comparer.Compare(left, right); // too slow

int comp = 0;
if (left < right)
    comp = -1;
else if (left > right)
    comp = 1;

In my testing this was about 15% faster.