Slowed Data reader in C# I am writing code to update data in the sample table (i.e. stbl) atoms of a video/audio file and found that in my method UpdateValues slows down to almost a crawl when executed. The line values.Add(value) was determined to be where the greatest time was taken through the debugger. Stepping through the program it was determined to take ~10mS to process. This List&lt;int&gt; is not touched by any other portion of the program. No other code appears to affect this list during the process.

public byte[] UpdateValues(int offset, string varName)
{
    //Create initial list
    List&lt;int&gt; values = new List&lt;int&gt;();
    //Set initial position to offset from (size, name, flags, count)
    int position = ((varName == "stsz") ? 20 : 16);
    //temporary byte array
    byte[] tempSize = new byte[4];

    //continue until position is equal to length of data
    while (position &lt; this.data.Length)
    {
        //Get value from position
        Array.Copy(this.data, position, tempSize, 0, 4);

        if (BitConverter.IsLittleEndian)//Check for Endianess
            Array.Reverse(tempSize);//Reverse if necessary

        //update the value to include the new offset
        int value = BitConverter.ToInt32(tempSize, 0) + offset;

        //Add the new value to a list
        values.Add(value);//fixed from 'Values', no change in output time
        //increment the location
        position += 4;
    }

    //Sets the values back to the original location as updated
    return SetValues(values, varName);
}


The SetValues method is not slow and takes less than a second to complete:

public byte[] SetValues( List&lt;int&gt; values, string name )
{
    int location = 0;

    int _size = (values.Count * 4) + ((name == "stsz")? 20 : 16);
    this.data = new byte[_size];
    byte[] temp = BitConverter.GetBytes(_size);
    if (BitConverter.IsLittleEndian)
        Array.Reverse(temp);

    //add size into first 4
    Array.Copy(temp, 0, this.data, location, temp.Length);
    location += temp.Length;

    //add box type label
    temp = Encoding.ASCII.GetBytes(name);
    Array.Copy(temp, 0, this.data, location, temp.Length);
    location += temp.Length;

    //add FLAGS
    temp = new byte[(name == "stsz")? 8 : 4];//add 4 bytes of 0 flags and version
    Array.Copy(temp, 0, this.data, location, temp.Length);
    location += temp.Length;

    //add Entry Count
    temp = BitConverter.GetBytes(values.Count);
    if (BitConverter.IsLittleEndian)
        Array.Reverse(temp);
    Array.Copy(temp, 0, this.data, location, temp.Length);
    location += temp.Length;



    //Add list of offsets to total
    foreach (int a in values)
    {
        temp = BitConverter.GetBytes(a);
        if (BitConverter.IsLittleEndian)
            Array.Reverse(temp);
        Array.Copy(temp, 0, this.data, location, temp.Length);
        location += temp.Length;
    }
    return this.data;
}
There are several Comments that are unnecessary. You should get rid of them, since your code explains a few of those Comments.

Most C# coding standards suggest that you always use braces for if statements, so instead of this:


 if (BitConverter.IsLittleEndian)//Check for Endianess
        Array.Reverse(tempSize);//Reverse if necessary



you would format it like this:

if (BitConverter.IsLittleEndian)
{
    Array.Reverse(tempSize);
}