public async Task SaveAsync(string outputFile, CalculationResult result) { RemoveFileIfExists(outputFile); StringBuilder tempSb = new StringBuilder(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Results:"); for (int i = 0; i < result.Answers.Length; i++) { sb.AppendLine($"x{i + 1}: {result.Answers[i]}"); } sb.AppendLine("Matrix:"); for (int i = 0; i < result.Matrix.Size; i++) { for (int j = 0; j < result.Matrix.Size; j++) { tempSb.Append($"{result.Matrix.Variables[i, j]} "); } sb.AppendLine($"{tempSb} = {result.Matrix.Results[i]}"); tempSb.Clear(); } using (FileStream fileStream = File.Create(outputFile)) using (StreamWriter writer = new StreamWriter(fileStream)) { await writer.WriteAsync(sb.ToString()); } }
private static async Task PerformTesting( int[] sizes, MatrixProvider provider, ResultsSaver saver, IEquationSolver solver, Action <Matrix> matrixAction = null) { Stopwatch stopwatch = new Stopwatch(); foreach (int size in sizes) { Matrix matrix = provider.GetMatrix(size); if (matrixAction != null) { matrixAction(matrix); } stopwatch.Start(); CalculationResult result = solver.Solve(matrix); stopwatch.Stop(); Console.WriteLine($"{size}x{size} ({result.Answers.Length}): {stopwatch.ElapsedMilliseconds}ms, {stopwatch.ElapsedTicks} ticks"); stopwatch.Reset(); string outputFile = string.Format(OutputFileTemplate, $"{size}x{size}", solver.GetType()); await saver.SaveAsync(outputFile, result); } }