static internal Tuple <double, double> time_it_ns <T>(string[] lines, ParsingFunc sut, long repeat) { double average = 0; double min_value = double.MaxValue; // warmup for (int i = 0; i != 100; i++) { sut(lines); } Stopwatch sw = new Stopwatch(); for (int i = 0; i != repeat; i++) { // ...; sw.Restart(); sut(lines); sw.Stop(); var dif = sw.ElapsedMilliseconds * 1000000; average += dif; min_value = min_value < dif ? min_value : dif; } average /= repeat; return(new Tuple <double, double>(min_value, average)); }
public IEnumerable <T> ParseFile <T>(string path, ParsingFunc <T> lineParser) { using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[16000 * 8]; // 128 KB int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { var slice = new ReadOnlySpan <byte>(buffer, start: 0, length: bytesRead); // bytesRead can be != buffer.Length int newLineLength = 0; while ((newLineLength = slice.IndexOf((byte)0x0A)) > 0) // 0x0A = new line { ReadOnlySpan <byte> utf8Line = slice.Slice(0, newLineLength); yield return(lineParser(utf8Line)); slice = slice.Slice(start: newLineLength + 1); } fileStream.Seek(-1 * slice.Length, SeekOrigin.Current); // we go back to the line end } } }
private static bool IsParseable <T>(string s, ParsingFunc <T> parsingFunc) where T : struct { T result = default(T); if (parsingFunc(s, out result)) { return(true); } return(false); }
// Métodos auxiliares private static T TryParse <T>(string s, ParsingFunc <T> parsingFunc, T defaultValue) where T : struct { T result = defaultValue; if (parsingFunc(s, out result)) { return(result); } return(defaultValue); }
static internal Tuple <double, double, double> time_it_ns <T>(string[] lines, ParsingFunc sut, long repeat) { double average = 0; double average_tpr = 0; // tick per run double min_value = double.MaxValue; // warmup for (int i = 0; i != 250; i++) { sut(lines); } Stopwatch sw = new Stopwatch(); for (int i = 0; i != repeat; i++) { // ...; sw.Restart(); sut(lines); sw.Stop(); // NOTE : Elapsed.TotalMilliseconds (double) returns the total number of whole and fractional milliseconds elapsed since inception // Elapsed.Milliseconds (int) returns the number of whole milliseconds in the current second => this is absolutely not what we need ! var dif = sw.Elapsed.TotalMilliseconds * 1000000; average += dif; average_tpr += sw.ElapsedTicks; min_value = min_value < dif ? min_value : dif; } average /= repeat; average_tpr /= repeat; return(new Tuple <double, double, double>(min_value, average, average_tpr)); }