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 3870844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:46:00+00:00 2026-05-19T21:46:00+00:00

struct mydata { public int id; public string data; } class Program { static

  • 0
struct mydata
{
    public int id;
    public string data;
}

class Program
{
    static void Main(string[] args)
    {
        List<mydata> myc = new List<mydata>();

        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        for (int i = 0; i < 1000000; i++)
        {
            mydata d = new mydata();

            d.id = i;
            d.data = string.Format("DataValue {0}",i);

            myc.Add(d);
        }

        stopwatch.Stop();
        Console.WriteLine("End: {0}", stopwatch.ElapsedMilliseconds);
}

Whys is this code above so SLOW..?
On an older laptop the times are:
C# code above: 1500ms
Similar code in Delphi: 450ms….

I then changed the code to a KeyValue/Pair (see below):

Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        var list = new List<KeyValuePair<int , string>>();

        for (int i = 0; i < 1000000; i++)
        {
            list.Add(new KeyValuePair<int,string>(i, "DataValue" + i));
        }

        stopwatch.Stop();
        Console.WriteLine("End: {0}", stopwatch.ElapsedMilliseconds);
        Console.ReadLine();

This improved the time to 1150ms..

If I remove the '+ i' the time is < 300ms

If I try and replace it with a StringBuilder, the timing is similar.

        StringBuilder sb = new StringBuilder();
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        var list = new List<KeyValuePair<int, string>>();

        for (int i = 0; i < 1000000; i++)
        {
            sb.Append("DataValue");
            sb.Append(i);
            list.Add(new KeyValuePair<int, string>(i, sb.ToString()));
            sb.Clear();
        }

        stopWatch.Stop();
        Console.WriteLine("End: {0}", stopWatch.ElapsedMilliseconds);
        Console.ReadLine();

Is slightly better.. If you remove the sb.Append(i) its very fast..

It would appear that any time you have to add an Int to a string/stringbuilder its VERY SLOW..

Can I speed this up in any way ??

EDIT **

The code below is the quickest I can get after making suggestions:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Threading;

namespace ConsoleApplication1 { struct mydata { public int id; public string data; }

class Program
{
    static void Main(string[] args)
    {
        List<mydata> myc = new List<mydata>();

        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        for (int i = 0; i < 1000000; i++)
        {
           mydata d = new mydata();
           d.id = i;
           d.data = "DataValue " + i.ToString();
           myc.Add(d);
        }

        stopwatch.Stop();
        Console.WriteLine("End: {0}", stopwatch.ElapsedMilliseconds);
        Console.ReadLine();
    }

}

}

If I replace the line:


  d.data = "DataValue " + i.ToString();


with:


  d.data = "DataValue ";

On my home machine this goes from 660ms -> 31ms..

Yes.. its 630ms slower with the '+ i.ToString()'

But still 2x faster than boxing/string.format etc etc..


            Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        var list = new List<KeyValuePair<int, string>>();

        for (int i = 0; i < 1000000; i++)
        {
            list.Add(new KeyValuePair<int, string>(i, "DataValue" +i.ToString()));
        }

        stopwatch.Stop();
        Console.WriteLine("End: {0}", stopwatch.ElapsedMilliseconds);
        Console.ReadLine();

is 612ms.. (no difference in speed if List>(1000000); is pre-initialised).

  • 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-19T21:46:01+00:00Added an answer on May 19, 2026 at 9:46 pm

    The problem with your first two examples is that the integer must first be boxed and then converted to a string. The boxing causes the code to be slower.

    For example, in this line:

    d.data = string.Format("DataValue {0}", i);
    

    the second parameter to string.Format is object, which causes boxing of i. See the intermediate language code for confirmation of this:

    ...
    box int32
    call string [mscorlib]System.String::Format(string, object)
    ...
    

    Similarly this code:

    d.data = "DataValue " + i;
    

    is equivalent to this:

    d.data = String.Concat("DataValue ", i);
    

    This uses the overload of String.Concat with parameters of type object so again this involves a boxing operation. This can be seen in the generated intermediate language code:

    ...
    box int32
    call string [mscorlib]System.String::Concat(object, object)
    ...
    

    For better performance this approach avoids the boxing:

    d.data = "DataValue " + i.ToString();
    

    Now the intermediate language code doesn’t include the box instruction and it uses the overload of String.Concat that takes two strings:

    ...
    call instance string [mscorlib]System.Int32::ToString()
    call string [mscorlib]System.String::Concat(string, string)
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
struct elem { int i; char k; }; elem user; // compile error! struct
Say I have a struct s with an int pointer member variable i. I
If I define a struct in C# using automatic properties like this: public struct
I have a c# struct where I need to forbid calling the no args
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have a struct defined like follows as part of an object. I'm trying
I have a structure: struct pkt_ { double x; double y; double alfa; double

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.