Is this benchmark test correct. Array vs List (0 ms vs 1100 ms)

  • Thread starter Thread starter Silvers11
  • Start date Start date
S

Silvers11

Guest
Hello!

I just like to ask if this benchmark test that I have done really is correct because the difference is amazing if this really is correct.

As seen I am calling func passing on a List and returning a List

As seen I am calling func passing on an array and returning an array

I that 1,000,000 times and the benchmark is the below. Is it really true that for the array it takes 0 milliseconds and for the List 1100 milliseconds. The difference is Huge and if that is true. Arrays seems to be much more faster than Lists?

Thank you!

private void button4_Click(object sender, EventArgs e)
{
new Thread(benchmarktest1).Start();
}
void benchmarktest1()
{
int[,] arraynums = new int[300, 100000]; int[,] arraynumsOUT = new int[300, 100000];
List<List<int>> nums = new List<List<int>>(); List<List<int>> numsOUT = new List<List<int>>(); String times = "";
for (int i = 0; i < 300; i++)
{
nums.Add(new List<int>());
for (int i2 = 0; i2 < 100000; i2++)
{
nums.Add(i2);
arraynums[i, i2] = i2;
}
}

Stopwatch stopw = new Stopwatch(); stopw.Start();
for (int i = 0; i < 1000000; i++)
{
func(nums, out numsOUT);
}
stopw.Stop(); times = stopw.ElapsedMilliseconds.ToString() + " milliseconds\n"; //1100 milliseconds


stopw.Reset(); stopw.Start();
for (int i = 0; i < 1000000; i++)
{
func2(arraynums, out arraynumsOUT);
}
stopw.Stop(); times += stopw.ElapsedMilliseconds.ToString() + " milliseconds\n"; // milliseconds
MessageBox.Show(times);
}
void func(List<List<int>> nums, out List<List<int>> numsOUT)
{
numsOUT = new List<List<int>>(nums);
}
void func2(int[,] arraynums, out int[,] arraynumsOUT)
{
arraynumsOUT = arraynums;
}

Continue reading...
 
Back
Top