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

  • SEARCH
  • Home
  • 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 509917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:03:37+00:00 2026-05-13T07:03:37+00:00

Using C#, I am noticing a significant difference in perfomance when populating a list

  • 0

Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects.

Each method performs differently:

Button1: 15 milliseconds

Button2: 31 milliseconds

Button3 & 4: 300 milliseconds

Note, the code for button 3 & 4 came from this topic

Could anyone explain why the dynamically created object is slower?

    public struct DataRow
    {
        public double t;
        public double vf;
        public double im;

        public double T { get { return t; } set { t = value; } }
        public double Vf { get { return vf; } set { vf = value; } }
        public double Im { get { return im; } set { im = value; } }
    }

    //Use struct defined above
    private void button1_Click(object sender, EventArgs e)
    {
        int n = 0;

        //adding rows
        List<DataRow> myTable = new List<DataRow>();
        DataRow myRow = new DataRow();

        start = DateTime.Now;

        while (n < 100000)
        {
            myRow.T = n * 1.0;
            myRow.Vf = 2.0;
            myRow.Im = 4.0;

            myTable.Add(myRow);

            n++;
        }
        end = DateTime.Now;
        System.TimeSpan diff = end.Subtract(start);
        label2.Text = diff.Seconds.ToString();
        label4.Text = diff.Milliseconds.ToString();

        dataGridView1.DataSource = myTable;
    }

    //define the list as it is done on buttons 3 & 4 but use the static struct
    private void button2_Click(object sender, EventArgs e)
    {
        Type myType = typeof(DataRow);

        Type listType = typeof(List<>);

        Type myListType = listType.MakeGenericType(myType);

        IList myTable = (IList)Activator.CreateInstance(myListType);

        DataRow bRow = new DataRow();

        int n = 0;
        start = DateTime.Now;
        while (n < 100000)
        {
            bRow.t = n * 1.0;
            bRow.vf = 2.0;
            bRow.im = 4.0;
            myTable.Add(bRow);

            n++;
        }
        end = DateTime.Now;
        System.TimeSpan diff = end.Subtract(start);
        label2.Text = diff.Seconds.ToString();
        label4.Text = diff.Milliseconds.ToString();
        dataGridView1.DataSource = myTable;

    }

    //Create assy at runtime and load dll
    private void button3_Click(object sender, EventArgs e)
    {
        Type myType = CreateDynRow();
        Assembly myAssy = Assembly.LoadFrom("DynaRowAssy.dll");
        Type myRow = myAssy.GetType("DynaRow");

        Type listType = typeof(List<>);

        Type myListType = listType.MakeGenericType(myRow);

        IList myTable = (IList)Activator.CreateInstance(myListType);

        FieldInfo piT = myRow.GetField("t");
        FieldInfo piVf = myRow.GetField("vf");
        FieldInfo piIm = myRow.GetField("im");

        ValueType aRow = (ValueType)Activator.CreateInstance(myRow);

        int n = 0;
        start = DateTime.Now;
        while (n < 100000)
        {
            piT.SetValue(aRow, 1 * n);
            piVf.SetValue(aRow, 2.0);
            piIm.SetValue(aRow, 4.0);
            myTable.Add(aRow);

            n++;
        }
        end = DateTime.Now;
        System.TimeSpan diff = end.Subtract(start);
        label2.Text = diff.Seconds.ToString();
        label4.Text = diff.Milliseconds.ToString();
        dataGridView1.DataSource = myTable;
    }

    //create assy at runtime in memory
    private void button4_Click(object sender, EventArgs e)
    {
        //build the assembly
        Type myType = CreateDynRow();

        Type listType = typeof(List<>);

        Type myListType = listType.MakeGenericType(myType);

        IList myTable = (IList)Activator.CreateInstance(myListType);

        FieldInfo piT = myType.GetField("t");
        FieldInfo piVf = myType.GetField("vf");
        FieldInfo piIm = myType.GetField("im");

        ValueType aRow = (ValueType)Activator.CreateInstance(myType);

        int n = 0;
        start = DateTime.Now;
        while (n < 100000)
        {
            piT.SetValue(aRow, 1 * n);
            piVf.SetValue(aRow, 2.0);
            piIm.SetValue(aRow, 4.0);
            myTable.Add(aRow);

            n++;
        }
        end = DateTime.Now;
        System.TimeSpan diff = end.Subtract(start);
        label2.Text = diff.Seconds.ToString();
        label4.Text = diff.Milliseconds.ToString();
        dataGridView1.DataSource = myTable;
    }
  • 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-13T07:03:38+00:00Added an answer on May 13, 2026 at 7:03 am

    It’s not (primarily) the dynamic creation: it’s the use of reflection (FieldInfo.SetValue) that makes the button3 and button4 versions slower than when the calls can be compiled in.

    A possible way around this is to declare an interface that your code can compile against, and have the dynamic type implement this interface. You’ll still take a small hit instantiating and querying the dynamic type for that interface via reflection, but after that it should be as fast as the “static” references.

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

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since your item variable is declared to be of the… May 15, 2026 at 4:47 am
  • Editorial Team
    Editorial Team added an answer You could always use .htaccess to rewrite the url and… May 15, 2026 at 4:47 am
  • Editorial Team
    Editorial Team added an answer For future reference, I have to include the HTML5 way… May 15, 2026 at 4:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.