public async Task Execute(string[] args) { await Parser.Default.ParseArguments <Options>(args).MapResult(async(Options opts) => { ISortingAlgorithm algorithm = GetSortingAlgorithm(opts.sortingAlgorithm); string[] linesIn = File.ReadAllLines(opts.inputFile); Stopwatch stopWatch = new Stopwatch(); string[] linesOut; Console.WriteLine("Sorting..."); double[] items = linesIn.Select(line => double.Parse(line)).ToArray(); stopWatch.Start(); if (opts.parallel) { await algorithm.SortParallel(items); } else { algorithm.Sort(items); } stopWatch.Stop(); linesOut = items.Select(item => item.ToString()).ToArray(); File.WriteAllLines(opts.outputFile, linesOut); ProgramUtils.WriteLineImpressive($"Succesfully sorted the values in {opts.inputFile} and wrote the result to {opts.outputFile} in {stopWatch.ElapsedMilliseconds} ms"); return(1); }, (_) => Task.FromResult(0)); }
public void Execute() { SortingType sortingType = GetSortingType(); if (sortingType == SortingType.Exit) { return; } ISortingAlgorithm algorithm = GetSortingAlgorithm(sortingType); Console.WriteLine("What's the input file name?"); string inputFilename = Console.ReadLine(); string[] linesIn = File.ReadAllLines(inputFilename); Console.WriteLine("What's the output file name?"); string outputFilename = Console.ReadLine(); Stopwatch stopWatch = new Stopwatch(); string[] linesOut; Console.WriteLine("Sorting..."); double[] items = linesIn.Select(line => double.Parse(line)).ToArray(); stopWatch.Start(); algorithm.Sort(items); stopWatch.Stop(); linesOut = items.Select(item => item.ToString()).ToArray(); File.WriteAllLines(outputFilename, linesOut); ProgramUtils.WriteLineImpressive($"Succesfully sorted the values in {inputFilename} and wrote the result to {outputFilename} in {stopWatch.ElapsedMilliseconds} ms"); }
public void Execute() { Console.WriteLine("How many entries do you want to generate?"); int entries = ProgramUtils.ReadLineInt(); Console.WriteLine("What's the minimum double?"); double min = ProgramUtils.ReadLineDouble(); Console.WriteLine("What's the maximum double?"); double max = ProgramUtils.ReadLineDouble(); Console.WriteLine("What's the output file name?"); string filename = Console.ReadLine(); DoubleGenerator generator = new DoubleGenerator(); double[] data = generator.GenerateData(entries, min, max); string[] lines = Array.ConvertAll(data, i => i.ToString()); File.WriteAllLines(filename, lines); ProgramUtils.WriteLineImpressive($"Succesfully wrote {entries} random integers in range [{min} - {max}] to {filename}!"); }
public void Execute() { // Create sorting algorithm SortingType sortingType = GetSortingType(); if (sortingType == SortingType.Exit) { return; } ISortingAlgorithm algorithm = GetSortingAlgorithm(sortingType); // Get parameters Console.WriteLine("How many runs?"); int runs = ProgramUtils.ReadLineInt(); Console.WriteLine("How many entries per run?"); int amount = ProgramUtils.ReadLineInt(); // Start runs Stopwatch stopWatch = new Stopwatch(); IntegerGenerator generator = new IntegerGenerator(); Console.WriteLine("Sorting..."); long[] results = new long[runs]; for (int i = 0; i < runs; i++) { int[] items = generator.GenerateData(amount, -1_000_000, 1_000_000); stopWatch.Start(); algorithm.Sort(items); stopWatch.Stop(); results[i] = stopWatch.ElapsedMilliseconds; stopWatch.Reset(); Console.Write($"\rProgress: {i+1}/{runs}"); } ProgramUtils.WriteLineImpressive($"The average sorting time of {runs} runs is: {results.Average()} ms"); }