C#排序和排序依据比较 [英] C# Sort and OrderBy comparison

查看:300
本文介绍了C#排序和排序依据比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用排序或排序依据排序列表。哪一个是更快?都工作在同一个 算法?

 名单,其中,人物>人=新的名单,其中,人物>();
persons.Add(新的Person(P0​​05,詹森));
persons.Add(新的Person(P0​​02,亚拉文));
persons.Add(新的Person(P0​​07,Kazhal));
 

1。

  persons.Sort((P1,P2)=>的String.Compare(p1.Name,p2.Name,真));
 

2。

  VAR的查询= persons.OrderBy(N => n.Name,新NameComparer());

类NameComparer:的IComparer<字符串>
{
    公众诠释比较(字符串x,y字符串)
    {
      返回的String.Compare(X,Y,真);
    }
}
 

解决方案

为什么不衡量它:

 类节目
{
    类NameComparer:的IComparer<字符串>
    {
        公众诠释比较(字符串x,y字符串)
        {
            返回的String.Compare(X,Y,真);
        }
    }

    类Person
    {
        公众人物(字符串ID,字符串名称)
        {
            ID = ID;
            名称=名称;
        }
        公共字符串ID {获得;组; }
        公共字符串名称{;组; }
    }

    静态无效的主要()
    {
        名单<人>人=新的名单,其中,人物>();
        persons.Add(新的Person(P0​​05,詹森));
        persons.Add(新的Person(P0​​02,亚拉文));
        persons.Add(新的Person(P0​​07,Kazhal));

        排序(人);
        排序依据(人);

        const int的COUNT = 1000000;
        秒表手表= Stopwatch.StartNew();
        的for(int i = 0; I<计数;我++)
        {
            排序(人);
        }
        watch.Stop();
        Console.WriteLine(排序:{0}毫秒,watch.ElapsedMilliseconds);

        观看= Stopwatch.StartNew();
        的for(int i = 0; I<计数;我++)
        {
            排序依据(人);
        }
        watch.Stop();
        Console.WriteLine(排序依据:{0}毫秒,watch.ElapsedMilliseconds);
    }

    静态无效排序(表<人>名单)
    {
        list.Sort((P1,P2)=>的String.Compare(p1.Name,p2.Name,真));
    }

    静态无效的OrderBy(名单<人>名单)
    {
        变种结果= list.OrderBy(N => n.Name,新NameComparer())的ToArray()。
    }
}
 

在我的电脑的时候在Release模式下编译该程序打印:

 排序:1162ms
排序依据:1269ms
 


更新:

所建议的@Stefan这里有排序的大名单的次数更少的结果:

 名单,其中,人物>人=新的名单,其中,人物>();
的for(int i = 0; I< 100000;我++)
{
    persons.Add(新的人(P+ i.ToString(),詹森+ i.ToString()));
}

排序(人);
排序依据(人);

const int的COUNT = 30;
秒表手表= Stopwatch.StartNew();
的for(int i = 0; I<计数;我++)
{
    排序(人);
}
watch.Stop();
Console.WriteLine(排序:{0}毫秒,watch.ElapsedMilliseconds);

观看= Stopwatch.StartNew();
的for(int i = 0; I<计数;我++)
{
    排序依据(人);
}
watch.Stop();
Console.WriteLine(排序依据:{0}毫秒,watch.ElapsedMilliseconds);
 

打印:

 排序:8965ms
排序依据:8460ms
 

在这种情况下,它看起来像排序依据执行得更好。


UPDATE2:

和使用随机的名字:

 名单,其中,人物>人=新的名单,其中,人物>();
的for(int i = 0; I< 100000;我++)
{
    persons.Add(新的人(P+ i.ToString(),RandomString(5,真)));
}
 

其中:

 私有静态随机randomSeed =新的随机();
公共静态字符串RandomString(INT尺寸,布尔小写)
{
    VAR SB =新的StringBuilder(大小);
    INT开始=(小写)? 97:65;
    的for(int i = 0; I<大小;我++)
    {
        sb.Append((炭)(26 * randomSeed.NextDouble()+开始));
    }
    返回sb.ToString();
}
 

收益率:

 排序:8968ms
排序依据:8728ms
 

不过排序依据更快

I can sort a list using Sort or OrderBy. Which one is faster? Are both working on same algorithm?

List<Person> persons = new List<Person>();
persons.Add(new Person("P005", "Janson"));
persons.Add(new Person("P002", "Aravind"));
persons.Add(new Person("P007", "Kazhal"));

1.

persons.Sort((p1,p2)=>string.Compare(p1.Name,p2.Name,true));

2.

var query = persons.OrderBy(n => n.Name, new NameComparer());

class NameComparer : IComparer<string>
{
    public int Compare(string x,string y)
    {
      return  string.Compare(x, y, true);
    }
}

解决方案

Why not measure it:

class Program
{
    class NameComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return string.Compare(x, y, true);
        }
    }

    class Person
    {
        public Person(string id, string name)
        {
            Id = id;
            Name = name;
        }
        public string Id { get; set; }
        public string Name { get; set; }
    }

    static void Main()
    {
        List<Person> persons = new List<Person>();
        persons.Add(new Person("P005", "Janson"));
        persons.Add(new Person("P002", "Aravind"));
        persons.Add(new Person("P007", "Kazhal"));

        Sort(persons);
        OrderBy(persons);

        const int COUNT = 1000000;
        Stopwatch watch = Stopwatch.StartNew();
        for (int i = 0; i < COUNT; i++)
        {
            Sort(persons);
        }
        watch.Stop();
        Console.WriteLine("Sort: {0}ms", watch.ElapsedMilliseconds);

        watch = Stopwatch.StartNew();
        for (int i = 0; i < COUNT; i++)
        {
            OrderBy(persons);
        }
        watch.Stop();
        Console.WriteLine("OrderBy: {0}ms", watch.ElapsedMilliseconds);
    }

    static void Sort(List<Person> list)
    {
        list.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true));
    }

    static void OrderBy(List<Person> list)
    {
        var result = list.OrderBy(n => n.Name, new NameComparer()).ToArray();
    }
}

On my computer when compiled in Release mode this program prints:

Sort: 1162ms
OrderBy: 1269ms


UPDATE:

As suggested by @Stefan here are the results of sorting a big list fewer times:

List<Person> persons = new List<Person>();
for (int i = 0; i < 100000; i++)
{
    persons.Add(new Person("P" + i.ToString(), "Janson" + i.ToString()));
}

Sort(persons);
OrderBy(persons);

const int COUNT = 30;
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < COUNT; i++)
{
    Sort(persons);
}
watch.Stop();
Console.WriteLine("Sort: {0}ms", watch.ElapsedMilliseconds);

watch = Stopwatch.StartNew();
for (int i = 0; i < COUNT; i++)
{
    OrderBy(persons);
}
watch.Stop();
Console.WriteLine("OrderBy: {0}ms", watch.ElapsedMilliseconds);

Prints:

Sort: 8965ms
OrderBy: 8460ms

In this scenario it looks like OrderBy performs better.


UPDATE2:

And using random names:

List<Person> persons = new List<Person>();
for (int i = 0; i < 100000; i++)
{
    persons.Add(new Person("P" + i.ToString(), RandomString(5, true)));
}

Where:

private static Random randomSeed = new Random();
public static string RandomString(int size, bool lowerCase)
{
    var sb = new StringBuilder(size);
    int start = (lowerCase) ? 97 : 65;
    for (int i = 0; i < size; i++)
    {
        sb.Append((char)(26 * randomSeed.NextDouble() + start));
    }
    return sb.ToString();
}

Yields:

Sort: 8968ms
OrderBy: 8728ms

Still OrderBy is faster

这篇关于C#排序和排序依据比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆