Optimize your code to run faster

How can I optimize the following code so that it runs faster?

static void Main(string[] args) 
{
    String a = "Hello ";
    String b = " World! ";
    for (int i=0; i<20000; i++) 
    {
        a = a + b;
    }
    Console.WriteLine(a);
} 
+3
source share
9 answers

From the StringBuilder documentation:

Performance recommendations

Concat AppendFormat String StringBuilder. String . StringBuilder . , ; , , .

String StringBuilder , . , StringBuilder , StringBuilder . , String , String . . StringBuilder , ; , .

static void Main(string[] args) {
    String a = "Hello ";
    String b = " World! ";
    StringBuilder result = new StringBuilder(a.Length + b.Length * 20000);
    result.Append(a);
    for (int i=0; i<20000; i++) {
        result.Append(b);
    }
    Console.WriteLine(result.ToString());
} 
+33

, , , .

+3

(5 , ):

static void Main(string[] args) 
{
    Console.Write("Hello ");
    for (int i=0; i<20000; i++)
       Console.Write(" World! ");
    Console.Write(Environment.NewLine);
}

forehand ( 4 , ):

static void Main(string[] args) 
{
   String a = "Hello "; 
   String b = " World! ";

   int it = 20000;
   char[] result = new char[a.Length + it*b.Length];

   a.ToCharArray().CopyTo(result, 0);

   for (int i = 0; i < it; i++) 
      b.ToCharArray().CopyTo(result, a.Length + i * b.Length);

   Console.WriteLine(result);    
}
+3

, IO ( ), , , . .

, . , a b, 20000 b , , 3 , ( , ). 160,008 .

, , , StringBuilder, , .

, , , Console.Write . ( b , , , - , Console.Write, #), , .

- . , , , "!" 20 000 , () , "!" 4000 . , , , .

, "!" Console.Write, , .


, cmd, :

( 100 )

     724.5312500 - concat
      53.2187500 - direct
      30.3906250 - direct writing b x10
      30.3750000 - direct writing b x100
      30.3750000 - builder
      30.3750000 - builder writing b x100

, :

     205.0000000 - concat
       9.7031250 - direct
       1.0781250 - direct writing b x10
       0.5000000 - builder
       0.4843750 - direct writing b x100
       0.4531250 - builder writing b x100

:

, cmd.exe. , ( CPU) .

, - , StringBuilder . , Console.Write , .

gcc C99 Win XP:

    0.375 - direct ( fputs ( b, stdout ) 20000 times )
    0.171 - direct unrolled ( fputs ( b x 100, stdout ) 200 times )
    0.171 - copy to b to a buffer 20000 times then puts once

C IO, .net. .net / .

+1
static void Main(string[] args) 
{
    const String a = "Hello " +
        /* insert string literal here that contains " World! " 20000 times. */ ;
    Console.WriteLine(a);
}

, . , - , . , - , , /.

+1

MemoryStream , StringBuilder:

    static void Main(string[] args)
    {
        String a = "Hello ";
        String b = " World! ";

        System.IO.MemoryStream ms = new System.IO.MemoryStream(20000 * b.Length + a.Length);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(ms);

        sw.Write(a);
        for (int i = 0; i < 20000; i++)
        {
            sw.Write(b);
        }

        ms.Seek(0,System.IO.SeekOrigin.Begin);
        System.IO.StreamReader sr = new System.IO.StreamReader(ms);
        Console.WriteLine(sr.ReadToEnd());
    }
+1

, ?

static void Main(string[] args) {
    String a = "Hello ";
    String b = " World! ";
    int worldCount = 20000;
    StringBuilder worldList = new StringBuilder(b.Length * worldCount);
    worldList.append(b);
    StringBuilder result = new StringBuilder(a.Length + b.Length * worldCount);
    result.Append(a);

    while (worldCount > 0) {

       if ((worldCount & 0x1) > 0) {  // Fewer appends, more ToStrings.
          result.Append(worldList);   // would the ToString here kill performance?
       }
       worldCount >>= 1;
       if (worldCount > 0) {
          worldList.Append(worldList);
       }
    }

    Console.WriteLine(result.ToString());
}
0

, String, . , , , , - . , stdout, ( ), .

0

. 20000 . , . , 10 . 4, , . "Parallize it" , 4 , (, , ). .

656 6658 66999 370717 hello loop. .

658 6641 65807 554546 build with stringbuilder,

664 6571 65676 314140 build with stringbuilder

2761 367042 OP, only lines (killed test during concatenation, nothing is printed on the screen)

167 43227 parallize it OP no output

27 40 323 1758 parallize it stringbuilder no output

0
source

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


All Articles