c# – 2个阵列中的Linq在1个循环中?

栏目: C# · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/2093532/linq-on-2-arrays-in-1-loop

是否有可能提高这些linq请求的效率?我用两个不同的循环……

你能帮我优化一下这段代码吗?

double[] x = { 2, 3, 1, 5, 7, 2, 3 };
        double[] y = { 1, 2, 3, 4, 5, 6, 7 };            
        IEnumerable<int> range = Enumerable.Range(0, x.Length);
        double[] y_sorted = (from n in range orderby x[n] select y[n]).ToArray();
        double[] x_sorted = (from n in range orderby x[n] select x[n]).ToArray();

python中的这段代码就像你喜欢的那样:

x_index = argsort(x)
        x_sorted = [x[i] for i in x_index]
        y_sorted = [y[i] for i in x_index]

你会注意到,在这个 python 代码中,我只使用一种.这不是c#代码的情况.

我们应该到最后:

x_sorted = { 1, 2, 2, 3, 3, 5, 7 }
        y_sorted = { 3, 1, 6, 2, 7, 4, 5 }

弗雷德

编辑:

我使用Diadistis的程序(经过小的修正后)

所以我们走了:

Array.Sort(x,y)(0.05)是跟随(0.18)by的最快方式

int[] x_index = Enumerable.Range(0, x.Length).OrderBy(i => x[i]).ToArray();
        double[] x_sorted = x_index.Select(i => x[i]).ToArray();
        double[] y_sorted = x_index.Select(i => y[i]).ToArray();

其他解决方案在我的电脑上的时间消耗相当(~0.35).

如果有人有一个有趣的想法,我会对其进行分析并更新这篇文章.

没关系,但我更喜欢更简单的语法:

double[] x = { 2, 3, 1, 5, 7, 2, 3 };
double[] y = { 2, 3, 1, 5, 7, 2, 3 };

double[] x_sorted = x.OrderBy(d => d).ToArray();
double[] y_sorted = y.OrderBy(d => d).ToArray();

编辑:

唉……我没有发现这是一个关联数组排序.

double[] x = { 2, 3, 1, 5, 7, 2, 3 };
double[] y = { 1, 2, 3, 4, 5, 6, 7 };  

double[] y_sorted = y.Clone() as double[];
double[] x_sorted = x.Clone() as double[];

Array.Sort(x_sorted, y_sorted);

编辑2 1/2

还有一些性能测试:

public class Program
{
    delegate void SortMethod(double[] x, double[] y);

    private const int ARRAY_SIZE = 3000000;

    private static Random RandomNumberGenerator = new Random();

    private static double[] x = GenerateTestData(ARRAY_SIZE);
    private static double[] y = GenerateTestData(ARRAY_SIZE);

    private static double[] GenerateTestData(int count)
    {
        var data = new double[count];

        for (var i = 0; i < count; i++)
        {
            data[i] = RandomNumberGenerator.NextDouble();
        }

        return data;
    }

    private static void SortMethod1(double[] x, double[] y)
    {
        Array.Sort(x, y);
    }

    private static void SortMethod2(double[] x, double[] y)
    {
        IEnumerable<int> range = Enumerable.Range(0, x.Length);
        x = (from n in range orderby x[n] select y[n]).ToArray();
        y = (from n in range orderby x[n] select x[n]).ToArray();
    }

    private static void SortMethod3(double[] x, double[] y)
    {
        int[] x_index = 
            Enumerable.Range(0, x.Length).OrderBy(i => x[i]).ToArray();

        x = x_index.Select(i => x[i]).ToArray();
        y = x_index.Select(i => y[i]).ToArray();
    }

    private static void SortMethod4(double[] x, double[] y)
    {
        int[] range =
            Enumerable.Range(0, x.Length).OrderBy(i => x[i]).ToArray();

        var q = (
            from n in range
            orderby x[n]
            select new { First = x[n], Second = y[n] }).ToArray();

        x = q.Select(t => t.First).ToArray();
        y = q.Select(t => t.Second).ToArray();
    }

    private static void SortMethodPerformanceTest(SortMethod sortMethod)
    {
        double[] y_sorted = y.Clone() as double[];
        double[] x_sorted = x.Clone() as double[];

        var sw = new Stopwatch();

        sw.Start();
        sortMethod.Invoke(x_sorted, y_sorted);
        sw.Stop();

        Console.WriteLine(
            string.Format(
                "{0} : {1}",
                sortMethod.Method.Name,
                sw.Elapsed));
    }

    static void Main(string[] args)
    {
        Console.WriteLine("For array length : " + ARRAY_SIZE);
        Console.WriteLine("------------------------------");

        SortMethodPerformanceTest(SortMethod1);
        SortMethodPerformanceTest(SortMethod2);
        SortMethodPerformanceTest(SortMethod3);
        SortMethodPerformanceTest(SortMethod4);

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}

结果如下:

For array length : 3000000
------------------------------
SortMethod1 : 00:00:00.6088503 // Array.Sort(Array, Array)
SortMethod2 : 00:00:07.9583779 // Original
SortMethod3 : 00:00:04.5023336 // dtb's Linq Alternative
SortMethod4 : 00:00:06.6115911 // Christian's Linq Alternative

翻译自:https://stackoverflow.com/questions/2093532/linq-on-2-arrays-in-1-loop


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大数据之眼

大数据之眼

[德]尤夫娜·霍夫施泰特 / 陈巍 / 浙江文艺出版社 / 2018-5-7 / 68.00元

德国狂销10万册的大数据商业应用畅销书,经典之作《大数据时代》的姊妹篇。 该书在德语国家促发了一场关于大数据,人工智能与人的关系建构的大讨论。 德国大数据与人工智能领域权威,首度为中国读者亲笔作序。 在后大数据时代,如何维护自己的隐私,如何巧妙利用资源获得更多金钱? 一部对大数据发展所产生的问题进行思考和规避的先知式作品。 当智能机器欲“优化”我们,入侵我们的生活,统......一起来看看 《大数据之眼》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具