How many clock cycles does one unboxing operation take?
The little program attached below gives on my PC:
integer array: 0,046875
object array: 1,515625
With the uncertainty always present in that kind of micro
benchmarks, that says:
7 million box+unbox per second
Your stuff runs in 0.1 second. If we say that 1/100 overhead
is the acceptable, then you can box and unbox 7000 times in
your code.
Arne
using System;
using System.Collections.Generic;
namespace E
{
public class MainClass
{
private const int N = 10000000;
public static void Main(string[] args)
{
int[] ia = new int[N];
DateTime dt1 = DateTime.Now;
for(int i = 0; i < N; i++)
{
ia
= i;
}
for(int i = 0; i < N; i++)
{
if(ia != i)
{
Console.WriteLine("Oops");
Environment.Exit(1);
}
}
DateTime dt2 = DateTime.Now;
object[] oa = new object[N];
Console.WriteLine("integer array: " + (dt2 - dt1).TotalSeconds);
DateTime dt3 = DateTime.Now;
for(int i = 0; i < N; i++)
{
oa = i;
}
for(int i = 0; i < N; i++)
{
if((int)oa != i)
{
Console.WriteLine("Oops");
Environment.Exit(1);
}
}
DateTime dt4 = DateTime.Now;
Console.WriteLine("object array: " + (dt4 - dt3).TotalSeconds);
Console.ReadKey();
}
}
}