C # Acceleration for strings?

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);
}

Which code is higher than SLOW ..?
On an older laptop, time: C # code above: 1500 ms Similar code in Delphi: 450 ms ....

Then I changed the code to 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();

code>

This improved the time to 1150 ms.

If I delete '+ i', the time will be <300ms

If I try to replace it with StringBuilder, the time will be the same.



        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();

code>

A little better .. If you remove sb.Append (i) very quickly.

It looks like anytime you need to add an Int to the / stringbuilder line, it is VERY SLOW.

Can I speed it up?

EDIT **

Below is the fastest code I can get after a sentence:



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();
    }

}

}

code>

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();

- 612 .. ( , List > (1000000), ).

+3
4

, , . .

, :

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

string.Format object, i. . :

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

:

d.data = "DataValue " + i;

:

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

String.Concat object, -. :

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

:

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

box String.Concat, :

...
call instance string [mscorlib]System.Int32::ToString()
call string [mscorlib]System.String::Concat(string, string)
...
+10

:

... String.Format("DataValue {0}", i ) // ~1650ms
... String.Format("DataValue {0}", "") // ~1250ms
... new MyData {Id = i, Data = "DataValue {0}" + i} // ~1200ms

, .

, DataValue , get ToString() , .

public override string ToString()
{
    return "DataValue {0}" + Id;
}
+1

, . -, , , . Add, , , , var .

, , , , .ToString. , int .

- . , , , , .Format.

. . - . .ToString, , , concat.

, , , , . , , , ( ).

, :

, , , "" . , . mydata string AND int. , , concat. , . , .

0

List. . "", , (EnsureCapacitiy). , , COPY . , List.Add Reflector.

, 1 000 000 25 , .

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

. , !

,

0

Source: https://habr.com/ru/post/1790171/


All Articles