Hi,
Is it possible to improve the efficiency of those linq requests? I use two different loops... Can you help me to 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 code in python is like that 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 sort. that's not the case of this 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 use the program of Diadistis (after a small correction)
So here we go: Array.Sort(x, y) (0.05) is the fastest way following (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();
The other solutions are quite equivalent (~0.35) in time consumption on my pc.
If someone have an interesting idea, I will profile it and update this post.