From cd4d4137765d49cc15c66dcf062a03622ead13e3 Mon Sep 17 00:00:00 2001 From: NRG-Drink Date: Thu, 30 May 2024 22:22:53 +0200 Subject: [PATCH 1/2] chore: made optimizations --- .../NRG.Matrix.App/DisplayObject.cs | 108 +++++++++--------- NRG.Matrix.App/NRG.Matrix.App/Matrix.cs | 97 +++++++--------- .../NRG.Matrix.App/Models/Option.cs | 14 +-- NRG.Matrix.App/NRG.Matrix.App/Program.cs | 18 +-- .../Properties/launchSettings.json | 3 +- 5 files changed, 108 insertions(+), 132 deletions(-) diff --git a/NRG.Matrix.App/NRG.Matrix.App/DisplayObject.cs b/NRG.Matrix.App/NRG.Matrix.App/DisplayObject.cs index 548a739..494f871 100644 --- a/NRG.Matrix.App/NRG.Matrix.App/DisplayObject.cs +++ b/NRG.Matrix.App/NRG.Matrix.App/DisplayObject.cs @@ -4,70 +4,70 @@ namespace NRG.Matrix.App; public record DisplayObject { - private static readonly Random _random = new(); - private static readonly int _traceLength = 25; - private string? _symbol; - private string _lastRandom = GetRandomSymbol(); + private static readonly Random _random = new(); + private readonly int _traceLength = 20 + _random.Next(0, 11); + private string? _symbol; + private string _lastRandom = GetRandomSymbol(); - public DisplayObject(int xRange) - { - Pos = new Point(_random.Next(0, xRange), 0); - IsTrace = false; - Color = OtherColor; - } + public DisplayObject(int xRange) + { + Pos = new Point(_random.Next(0, xRange), 0); + IsTrace = false; + Color = OtherColor; + } - public DisplayObject(Point pos, int yOffset) - { - Pos = pos with { Y = pos.Y - yOffset }; - IsTrace = true; - Color = TraceColor; - } + public DisplayObject(Point pos, int yOffset) + { + Pos = pos with { Y = pos.Y - yOffset }; + IsTrace = true; + Color = TraceColor; + } - public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGreen; - public static ConsoleColor OtherColor { get; set; } = ConsoleColor.Gray; + public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGreen; + public static ConsoleColor OtherColor { get; set; } = ConsoleColor.Gray; - public Point Pos { get; init; } = new(999, 999); - public bool IsTrace { get; init; } = false; - public ConsoleColor Color { get; init; } = ConsoleColor.Gray; + public Point Pos { get; init; } = new(999, 999); + public ConsoleColor Color { get; init; } = ConsoleColor.Gray; + public bool IsTrace { get; init; } = false; - public string Symbol - { - get => _symbol is null ? GetNextRandomSymbol() : _symbol; - init => _symbol = value; - } + public string Symbol + { + get => _symbol is null ? GetNextRandomSymbol() : _symbol; + init => _symbol = value; + } - public DisplayObject Fall(int x = 0, int y = 1) - => this with { Pos = new Point(Pos.X + x, Pos.Y + y) }; + public DisplayObject Fall(int x = 0, int y = 1) + => this with { Pos = new Point(Pos.X + x, Pos.Y + y) }; - public IEnumerable Trace() - => Enumerable.Range(1, _traceLength) - .Select(GetTrace) - .Append(GetClear(_traceLength + 1)); + public IEnumerable Trace() + => Enumerable.Range(1, _traceLength) + .Select(GetTrace) + .Append(GetClear(_traceLength + 1)); - private DisplayObject GetTrace(int offset) - => new(Pos, offset); + private DisplayObject GetTrace(int offset) + => new(Pos, offset); - private DisplayObject GetClear(int offset) - => new(Pos, offset) { Symbol = " " }; + private DisplayObject GetClear(int offset) + => new(Pos, offset) { Symbol = " " }; - private string GetNextRandomSymbol() - { - var getRandom = ShouldGetNewRandom(); - if (!IsTrace && !getRandom) - { - // Double chance for changed symbol on non trace object. - getRandom = ShouldGetNewRandom(); - } - if (getRandom) - { - _lastRandom = GetRandomSymbol(); - } - return _lastRandom; - } + private string GetNextRandomSymbol() + { + var getRandom = ShouldGetNewRandom(); + if (!IsTrace && !getRandom) + { + // Double chance for changed symbol on non trace object. + getRandom = ShouldGetNewRandom(); + } + if (getRandom) + { + _lastRandom = GetRandomSymbol(); + } + return _lastRandom; + } - private static string GetRandomSymbol() - => ((char)_random.Next(33, 123)).ToString(); + private static string GetRandomSymbol() + => ((char)_random.Next(33, 123)).ToString(); - private static bool ShouldGetNewRandom() - => _random.Next(0, 3) == 0; + private static bool ShouldGetNewRandom() + => _random.Next(0, 3) == 0; } \ No newline at end of file diff --git a/NRG.Matrix.App/NRG.Matrix.App/Matrix.cs b/NRG.Matrix.App/NRG.Matrix.App/Matrix.cs index a27d521..8781338 100644 --- a/NRG.Matrix.App/NRG.Matrix.App/Matrix.cs +++ b/NRG.Matrix.App/NRG.Matrix.App/Matrix.cs @@ -1,68 +1,59 @@ -using System; -using System.Diagnostics; +using NRG.Matrix.App.Models; using System.Text; namespace NRG.Matrix.App; -public class Matrix(int delay = 80, TimeSpan? time = null, int maxObjects = 9999, Func? objectAddRate = null) +public class Matrix(Option option) { - private readonly bool _isEndlessMode = time is null; - - private List list = []; + private List _displayObjects = []; private (int Width, int Height) _windowDimension = (Console.WindowWidth, Console.WindowHeight); - private int _frames = 0; - private float _objectBuildup = 1; + private readonly float _addRate = option.AddRate; + private readonly int _delay = option.Delay < 0 ? 0 : option.Delay; + private readonly int _maxObjects = option.MaxObjects; + private float _objectBuildup = 1; public void Enter() { - delay = delay < 0 ? 0 : delay; - objectAddRate ??= e => e / 200.0f; try { Console.Clear(); - var sw = Stopwatch.StartNew(); - while (_isEndlessMode || sw.Elapsed < time) + while (true) { AddObjects(Console.WindowWidth); HandleObjects(Console.WindowHeight); PrintObjects(); - if (!_isEndlessMode) - { - HandleBenchmarkMode(); - } - - Task.Delay(delay).Wait(); + Task.Delay(_delay).Wait(); } } finally { - LeaveMatrix(delay, time); + LeaveMatrix(_delay); } } private void AddObjects(int width) { - if (maxObjects < list.Count) + if (_maxObjects < _displayObjects.Count) { return; } - _objectBuildup += objectAddRate!(width); + _objectBuildup += width * _addRate / 200; var objectsToAdd = (int)_objectBuildup; - for (int i = 0; i < objectsToAdd; i++) - { + for (int i = 0; i < objectsToAdd; i++) + { var obj = new DisplayObject(width); - list.Add(obj); - list.AddRange(obj.Trace()); - } + _displayObjects.Add(obj); + _displayObjects.AddRange(obj.Trace()); + } _objectBuildup -= objectsToAdd; } private void HandleObjects(int height) { - list = list + _displayObjects = _displayObjects .Select(e => e.Fall()) .Where(e => e.Pos.Y < height) .OrderBy(e => e.IsTrace) @@ -74,9 +65,8 @@ private void PrintObjects() { try { - HandleWindowSizeChange(); Console.CursorVisible = false; - var validColors = list.Where(e => e.Pos.Y >= 0).GroupBy(e => e.Color); + var validColors = _displayObjects.Where(e => e.Pos.Y >= 0).GroupBy(e => e.Color); var traces = validColors.FirstOrDefault(e => e.Key is ConsoleColor.DarkGreen); PrintTrace(traces); @@ -141,38 +131,39 @@ private void PrintOthers(IEnumerable> oth private void HandleWindowSizeChange() { var hasWindowSizeChanges = _windowDimension.Width != Console.WindowWidth || _windowDimension.Height != Console.WindowHeight; - if (hasWindowSizeChanges) + if (!hasWindowSizeChanges) { - Console.Clear(); - list = list - .Where(e => e.Pos.X < Console.WindowWidth) - .Where(e => e.Pos.Y < Console.WindowHeight) - .ToList(); - _windowDimension = (Console.WindowWidth, Console.WindowHeight); + return; } + Console.Clear(); + _displayObjects = _displayObjects + .Where(e => e.Pos.X < Console.WindowWidth) + .Where(e => e.Pos.Y < Console.WindowHeight) + .ToList(); + _windowDimension = (Console.WindowWidth, Console.WindowHeight); } - private void HandleBenchmarkMode() - { - Console.SetCursorPosition(5, 1); - Console.Write($"Number of objects in RAM: {list.Count} "); - _frames++; - } + //private void HandleBenchmarkMode() + //{ + // Console.SetCursorPosition(5, 1); + // Console.Write($"Number of objects in RAM: {_displayObjects.Count} "); + // _frames++; + //} - private void LeaveMatrix(int delay, TimeSpan? time) + private void LeaveMatrix(int delay, TimeSpan? time = null) { Console.CursorVisible = true; Console.ForegroundColor = ConsoleColor.Gray; Console.SetCursorPosition(0, Console.WindowHeight + 1); - if (!_isEndlessMode) - { - Console.WriteLine( - $"Rendered Frames: {_frames} " + - $"of possible {time!.Value.TotalMilliseconds / delay} " + - $"in time {time} (hh:MM:ss)\n" + - $"With an average calculation time of " + - $"{((time!.Value.TotalMilliseconds - delay * _frames) / _frames):0.0} ms per frame" - ); - } + //if (!_isEndlessMode) + //{ + // Console.WriteLine( + // $"Rendered Frames: {_frames} " + + // $"of possible {time!.Value.TotalMilliseconds / delay} " + + // $"in time {time} (hh:MM:ss)\n" + + // $"With an average calculation time of " + + // $"{((time!.Value.TotalMilliseconds - delay * _frames) / _frames):0.0} ms per frame" + // ); + //} } } diff --git a/NRG.Matrix.App/NRG.Matrix.App/Models/Option.cs b/NRG.Matrix.App/NRG.Matrix.App/Models/Option.cs index 3fd0a6c..0ad41d0 100644 --- a/NRG.Matrix.App/NRG.Matrix.App/Models/Option.cs +++ b/NRG.Matrix.App/NRG.Matrix.App/Models/Option.cs @@ -4,12 +4,10 @@ namespace NRG.Matrix.App.Models; public class Option { - [Option('d', "delay",Required = false, Default = 80, HelpText = "Delay in Milliseconds between two frames.")] - public int Delay { get; set; } - [Option('t', "time", Required = false, Default = null, HelpText = "How long the benchmark should run.")] - public TimeSpan? Time { get; set; } - [Option('m', "maxobjects", Required = false, Default = 9999, HelpText = "Maximum number of objects on the screen.")] - public int MaxObjects { get; set; } - [Option('a', "addrate", Required = false, HelpText = "Rate on how fast per frame the objects should spawn. (float)")] - public string AddRate { get; set; } = "e => 1"; + [Option('d', "delay", Required = false, Default = 80, HelpText = "Delay in Milliseconds between two frames (speed of falling objects).")] + public int Delay { get; init; } + [Option('o', "max-objects", Required = false, Default = 9999, HelpText = "Maximum number of objects on the screen (including traces).")] + public int MaxObjects { get; init; } + [Option('a', "add-rate", Required = false, Default = 1, HelpText = "Number of objects added each frame (float):")] + public float AddRate { get; init; } } diff --git a/NRG.Matrix.App/NRG.Matrix.App/Program.cs b/NRG.Matrix.App/NRG.Matrix.App/Program.cs index 860e098..225edaa 100644 --- a/NRG.Matrix.App/NRG.Matrix.App/Program.cs +++ b/NRG.Matrix.App/NRG.Matrix.App/Program.cs @@ -8,23 +8,9 @@ internal class Program static void Main(string[] args) { Parser.Default.ParseArguments