Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:47:37+00:00 2026-05-26T20:47:37+00:00

I have code that is adding ~100,000 items to a List. If I add

  • 0

I have code that is adding ~100,000 items to a List.

If I add an array of strings or objects the code runs almost instantly (under 100 ms), but if I try to add an array of structs, it takes almost 1.5 seconds just for the .Add calls.

Why is there such a performance impact when using a struct[]?

Here is my struct:

public struct LiteRowInfo
{
    public long Position;
    public int Length;
    public int Field;
    public int Row;

    public LiteRowInfo(long position, int length, int field, int row)
    {
        this.Position = position;
        this.Length = length;
        this.Field = field;
        this.Row = row;
    }
}

EDIT 2: Performances of the string method is faster than that of the struct:
I appreciate the comments, it does seem like there is additional overhead in creating the struct its self. I think I will just create 2 seperate list to store the position and length to improve performance.

private void Test()
    {
        Stopwatch watch = new Stopwatch();

        watch.Start();
        List<LiteRowInfo[]> structList = new List<LiteRowInfo[]>();

        for (int i = 0; i < 100000; i++)
        {
            LiteRowInfo[] info = new LiteRowInfo[20];

            for (int x = 0; x < 20; x++)
            {
                LiteRowInfo row;
                row.Length = x;
                row.Position = (long)i;
                info[x] = row;
            }
            structList.Add(info);
        }
        Debug.Print(watch.ElapsedMilliseconds.ToString());

        watch.Reset();
        watch.Start();

        List<string[]> stringList = new List<string[]>();

        for (int i = 0; i < 100000; i++)
        {
            string[] info = new string[20];

            for (int x = 0; x < 20; x++)
            {
                info[x] = "String";
            }
            stringList.Add(info);
        }

        Debug.Print(watch.ElapsedMilliseconds.ToString());
    }

EDIT: Here is all relevant code:
Note: If I comment out only the pos.Add(rowInfo); line, the performance is similar to that of a string[] or int[].

        private void executeSqlStream()
    {
        List<LiteRowInfo[]> pos = new List<LiteRowInfo[]>();

        long currentPos = 0;

        _stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(_stream);

        using (SqlConnection cnn = new SqlConnection(_cnnString))
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand(_sqlString, cnn);

            SqlDataReader reader = cmd.ExecuteReader();

            int fieldCount = reader.FieldCount;
            int rowNum = 0;
            UnicodeEncoding encode = new UnicodeEncoding();
            List<string> fields = new List<string>();
            for (int i = 0; i < fieldCount; i++)
            {
                fields.Add(reader.GetFieldType(i).Name);
            }
            while (reader.Read())
            {
                LiteRowInfo[] rowData = new LiteRowInfo[fieldCount];
                for (int i = 0; i < fieldCount; i++)
                {
                    LiteRowInfo info;
                    if (reader[i] != DBNull.Value)
                    {
                        byte[] b;
                        switch (fields[i])
                        {
                            case "Int32":
                                b = BitConverter.GetBytes(reader.GetInt32(i));
                                break;
                            case "Int64":
                                b = BitConverter.GetBytes(reader.GetInt64(i));
                                break;
                            case "DateTime":
                                DateTime dt = reader.GetDateTime(i);
                                b = BitConverter.GetBytes(dt.ToBinary());
                                break;
                            case "Double":
                                b = BitConverter.GetBytes(reader.GetDouble(i));
                                break;
                            case "Boolean":
                                b = BitConverter.GetBytes(reader.GetBoolean(i));
                                break;
                            case "Decimal":
                                b = BitConverter.GetBytes((float)reader.GetDecimal(i));
                                break;
                            default:
                                b = encode.GetBytes(reader.GetString(i));
                                break;
                        }
                        int len = b.Length;

                        info.Position = currentPos += len;
                        info.Length = len;
                        info.Field = i;
                        info.Row = rowNum;
                        currentPos += len;
                        _stream.Write(b, 0, len);
                    }
                    else
                    {
                        info.Position = currentPos;
                        info.Length = 0;
                        info.Field = i;
                        info.Row = rowNum;
                    }
                    rowData[i] = info;
                }
                rowNum++;
                pos.Add(rowData);
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:47:38+00:00Added an answer on May 26, 2026 at 8:47 pm

    Given that the array itself is a reference type, I very much doubt that you’re actually seeing what you think you’re seeing.

    I suspect the difference isn’t in adding an array reference to a list – I suspect it’s creating the array in the first place. Each array element will take more space than a reference, so you’re having to allocate more memory. That may well mean you’re also triggering garbage collection.

    To benchmark just List<T>.Add, I suggest that you repeatedly add a reference to the same array several times.

    As an aside, having an array as the list element type feels like a bit of a smell to me. There are times when that’s valid, but personally I would consider whether it’s actually something which could be encapsulated in another type.

    EDIT: You say you’ve posted all the relevant code, but that really isn’t benchmark code for List<T>.Add – it contains database access for one thing, which is almost certainly taking way longer than any of the in-memory manipulation!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that works great for adding a button to the toolbar: NSArray*
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE .
I have code that I want to compile on all unix systems, but if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.