Re: readonly arrays

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Family Tree Mike" <FamilyTreeMike@xxxxxxxxxxxxxxxx> wrote in message news:uM1I2ldSJHA.4916@xxxxxxxxxxxxxxxxxxxxxxx
"Mark" <mmodrall@xxxxxxxxxxxxx> wrote in message news:FE288528-7025-4E67-BCFF-DFCB4820A71F@xxxxxxxxxxxxxxxx
I've been reading many of the articles on why C# doesn't have true readonly
arrays and I was doing a little time testing.

I found the ReadOnlyCollection<T> = Array.AsReadOnly<T>(T[]) construct but
when I ran some time tests, I was appalled to see that simple read access was
between 8x and 9x slower than just using a regular array (for type char
anyway).

I threw together a simple wrapper:
public class RArray<T>
{
private T[] holdme;
public RArray(T[] init)
{
holdme = (T[])init.Clone();
}

public T this[int i]
{
get { return holdme[i]; }
set { throw new InvalidOperationException("No way"); }
}
}

And the same timing test had my wrapper at around 75% slower.

Using Reflector, I looked at ReadOnlyCollection<T>'s get method and saw it
was doing essentially the same thing my wrapper was, so I couldn't explain
why ReadOnlyCollection<T> was just so much slower.

Any ideas?

Thanks
Mark



I only get a factor of slightly more than two using the following test code:

class Program
{
static int ListSize = 10000000;
static int [] SearchIDs;
static char [] OriginalArray;
static ReadOnlyCollection<char> ROC;

static void Setup()
{
Random r = new Random();
OriginalArray = new char[ListSize];
SearchIDs = new int[ListSize];
for (long idx = 0; idx < ListSize; ++idx)
{
OriginalArray[idx] = (char) r.Next();
SearchIDs[idx] = r.Next(ListSize);
}
ROC = Array.AsReadOnly<char>(OriginalArray);
}

static long TimeTestNormal()
{
char val;
Stopwatch sw = new Stopwatch();
sw.Start();
foreach (int idx in SearchIDs)
val = OriginalArray[idx];
return sw.ElapsedMilliseconds;
}

static long TimeTestRO()
{
char val;
Stopwatch sw = new Stopwatch();
sw.Start();
foreach (int idx in SearchIDs)
val = ROC [idx];
return sw.ElapsedMilliseconds;
}

static void Main(string[] args)
{
Setup();
Console.Out.WriteLine(string.Format("Test Normal: {0} milliseconds.", TimeTestNormal()));
Console.Out.WriteLine(string.Format("Test Read Only: {0} milliseconds.", TimeTestRO()));
Console.In.ReadLine();
}
}



And yes, I know the initialization of the random character array is far from a good way to do that, but, it does not affect the timing...


.



Relevant Pages

  • Re: readonly arrays
    ... arrays and I was doing a little time testing. ... static int SearchIDs; ... foreach (int idx in SearchIDs) ...
    (microsoft.public.dotnet.framework)
  • Re: readonly arrays
    ... char array and referenced the last one 50 million times. ... static int SearchIDs; ... foreach (int idx in SearchIDs) ...
    (microsoft.public.dotnet.framework)
  • Re: readonly arrays
    ... char array and referenced the last one 50 million times. ... static int SearchIDs; ... foreach (int idx in SearchIDs) ...
    (microsoft.public.dotnet.framework)
  • Re: readonly arrays
    ... difference between my very simplistic ReadOnlyCollection and the real ... ReadOnlyCollection and a basic array (under my admittedly contrived ... static int SearchIDs; ... foreach (int idx in SearchIDs) ...
    (microsoft.public.dotnet.framework)
  • Re: random generator for a given period
    ... array such that the same index is never visited once (and consequently ... shuffle the first m integers) Call it List_m. ... static int[] array; ...
    (sci.math)