From 337e7976d13b1aea8ac8282c6bf440c2abbf202a Mon Sep 17 00:00:00 2001 From: Dyego Maas Date: Wed, 12 Jan 2022 09:50:35 -0300 Subject: [PATCH] Performance optimization, reducing the amount of writes to the console necessary to output each iteration's matrix. --- 55_Life/csharp/Program.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/55_Life/csharp/Program.cs b/55_Life/csharp/Program.cs index 47ed7386..17e6fb18 100644 --- a/55_Life/csharp/Program.cs +++ b/55_Life/csharp/Program.cs @@ -132,12 +132,14 @@ void ProcessSimulation() var nextMinX = maxHeight - 1; var nextMinY = maxWidth - 1; var nextMaxX = 0; - var nextMaxY = 0; + var nextMaxY = 0; + + var matrixOutput = new StringBuilder(); // prints the empty lines before search area for (var x = 0; x < minX; x++) { - Console.WriteLine(); + matrixOutput.AppendLine(); } // refreshes the matrix and updates search area @@ -168,14 +170,15 @@ void ProcessSimulation() nextMaxY = Math.Max(y + 1, nextMaxY); } - Console.WriteLine(string.Join(separator: null, values: printedLine)); + matrixOutput.AppendLine(string.Join(separator: null, values: printedLine)); } // prints empty lines after search area for (var x = maxX + 1; x < maxHeight; x++) { - Console.WriteLine(); + matrixOutput.AppendLine(); } + Console.WriteLine(matrixOutput); Console.WriteLine(); void UpdateSearchArea()