Performance issues with nested loops and string concatenations

前端 未结 8 726
轻奢々
轻奢々 2021-01-28 10:43

Can someone please explain why this code is taking so long to run (i.e. >24 hours): The number of rows is 5000, whilst the number of columns is 2000 (i.e. Approximately 10m loop

8条回答
  •  轮回少年
    2021-01-28 11:39

    Yes, the += operator is not very efficient. Use StringBuilder instead.

    In the .NET framework a string is immutable, which means it cannot be modified in place. This means the += operator has to create a new string every time, which means allocating memory, copying the value of the existing string and writing it to the new location. It's ok for one or two concatenations, but as soon as you put it in a loop you need to use an alternative.

    http://support.microsoft.com/kb/306822

    You'll see a massive performance improvement by using the following code:

    var textToWriteBuilder = new StringBuilder();
    
    for (int i = 0; i < m.rows; i++)
    {
        for (int j = 0; j < m.cols; j++)
        {
            textToWriteBuilder.Append(m[i, j].ToString() + ",");
        }
    
        // I've modified the logic on the following line, I assume you want to 
        // concatenate the value instead of overwriting it as you do in your question.
        textToWriteBuilder.Append(textToWriteBuilder.Substring(0, textToWriteBuilder.Length - 2));
        textToWriteBuilder.Append(Environment.NewLine);
    }
    
    string textToWrite = textToWriteBuilder.ToString();
    

提交回复
热议问题