Code: Select all
[Benchmark]
public long IterateSystemsByModel()
{
long count = 0;
IGalaxyParser parser = new GalaxyParser();
foreach (StarSystem system in parser.ParseToModel(inputFilename))
{
count++;
}
return count;
}
Code: Select all
public IEnumerable ParseToModel(string filename)
{
using (FileStream fs = File.OpenRead(filename))
{
using (StreamReader reader = new(fs))
{
while (!reader.EndOfStream)
{
var line = LineReader.GetNextLine(reader);
if (line[0] == '{') // Valid object
{
yield return JsonSerializer.Deserialize(line, serializerOptions) ??
throw new InvalidOperationException($"Line is not a valid star system.\nLine: {line}");
}
}
reader.Close();
}
fs.Close();
}
}
Code: Select all
public static string GetNextLine(StreamReader reader)
{
// This method combines Trim() plus Substring() (stripping off the trailing comma),
// making it significantly faster than trying to do it in two or three calls. Also,
// It uses about a quarter the memory.
// Go to the first non-whitespace character
while (char.IsWhiteSpace((char)reader.Peek()))
{
reader.Read();
}
// Read the rest of the line
List chars = [];
var c = (char)reader.Read();
while (c != '\n')
{
chars.Add(c);
c = (char)reader.Read();
}
// Trim
var pos = chars.Count - 1;
while (char.IsWhiteSpace(chars[pos]))
{
chars.RemoveAt(pos--);
}
// Strip trailing comma, or else JsonDocument.Parse will choke on it
if (chars[^1] == ',')
{
chars = chars[..^1];
}
return new string([.. chars]);
}
< /code>
(StarSystem
GC = Concurrent Workstation //
/> VektorSize = 256 // Job: defaultjob < /p>
Overheadjitting 1: 1 OP, 313300.00 ns, 313,3000 US /op < /p>
< /Blockquote>
, dann setzt es sich über 8GB -Memory -Suchverletzungen. Raum zum Essen) und stirbt. Zu diesem Zeitpunkt kann nur ein Internales für .NET oder b.) Die Tatsache, dass Linerader statisch ist. Sollte ich hier angesichts der von Natur aus eine inhärent basierende Bibliothek auf IO-basierte Bibliothek handelt, da ich stattdessen meine eigenen einfacheren Leistungsnummern hier rollen? (d. H. Schreiben Sie einen Test, bei dem die Methode 10000 Mal ausgeführt wird, den größten und kleinsten Ausreißer auswirken und den Rest durchschnittlich)? Oder gibt es eine bessere Bibliothek für so etwas?