Here is my simple benchmark of three methods proposed by users:
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double[] input = Enumerable.Range(0, 10000000).Select(i => (double)i).ToArray();
while (true)
{
sw.Start();
LinqStack(input);
sw.Stop();
Console.WriteLine("LinqStack(): {0}ms", sw.ElapsedMilliseconds);
sw.Restart();
SimpleStack(input);
sw.Stop();
Console.WriteLine("SimpleStack(): {0}ms", sw.ElapsedMilliseconds);
sw.Restart();
OriginalStack(input);
sw.Stop();
Console.WriteLine("OriginalStack(): {0}ms", sw.ElapsedMilliseconds);
sw.Reset();
Console.ReadLine();
}
}
/// <summary>
/// By Sriram Sakthivel
/// </summary>
static double[] LinqStack(params double[] input)
{
return input.Concat(input.Reverse())
.ToArray();
}
static double[] SimpleStack(params double[] input)
{
int length = input.Length;
double[] output = new double[length * 2];
for (int i = 0; i < length; i++)
{
output[i] = input[i];
}
for (int i = 0; i < length; i++)
{
output[i + length] = input[length - i - 1];
}
return output;
}
/// <summary>
/// By p.s.w.g
/// </summary>
static double[] OriginalStack(params double[] input)
{
int N = input.Length; // assuming input = { 1, 2, 3 }
var z = new double[2 * N]; // z = { 0, 0, 0, 0, 0, 0 }
input.CopyTo(z, 0); // z = { 1, 2, 3, 0, 0, 0 }
input.CopyTo(z, N); // z = { 1, 2, 3, 1, 2, 3 }
Array.Reverse(z, N, N); // z = { 1, 2, 3, 3, 2, 1 }
return z;
}
Results:
LinqStack(): 647ms SimpleStack(): 184ms OriginalStack(): 66ms
LinqStack(): 710ms SimpleStack(): 175ms OriginalStack(): 66ms
LinqStack(): 602ms SimpleStack(): 334ms OriginalStack(): 95ms
As you see, while the linq method is the simplest and most readable, it is not the most efficient. Using self-written loop also is not the best idea if you worry about performance. Standart array operations are faster.