Is it possible to increase the efficiency of these linq queries? I use two different loops ... Can you help me optimize this code?
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();
This python code looks like this if you prefer:
x_index = argsort(x) x_sorted = [x[i] for i in x_index] y_sorted = [y[i] for i in x_index]
You will notice that in this python code I use only one view. which is not the case with C # code.
we should get at the end:
x_sorted = { 1, 2, 2, 3, 3, 5, 7 } y_sorted = { 3, 1, 6, 2, 7, 4, 5 }
Fred
Edit: I am using Diadistis program (after a little correction)
So, let's go: Array.Sort (x, y) (0,05) is the fastest way following (0.18) on
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();
Other solutions are pretty equivalent (~ 0.35) in temporary consumption on my PC.
If anyone has an interesting idea, I will profile it and update this post.