by deltaci · 4y ago · 0 comments
using var file = File.OpenRead("file.bin");
var counter = 0;
var sw = Stopwatch.StartNew();
var buf = new byte[4096];
while (file.Read(buf,0,buf.Length) > 0)
{
foreach (var t in buf)
{
if (t == '1')
{
counter++;
}
}
}
sw.Stop();
Console.WriteLine($"Counted {counter:N0} 1s in {sw.Elapsed.TotalMilliseconds:N4} milliseconds");(This bug won't affect the performance comparison, but I was just reminded of how error prone these kinds APIs can be vs the PHP/Python route of having the library function just allocate a new buffer each time.)
int len;
while ((len=file.Read(buf,0,buf.Length)) > 0) {
for (int i=0; i<len; i++_) {
if (buf[i] == '1') {
counter++;
}
}
}My C# is very rusty (no pun intended) but I would guess the core of the program could be something like
File.ReadAllBytes("file.bin").Where(x => x == '1').Count()
And nitpick: the code you gave has a bug. file.Read can return less than 4096. If so, you should only loop over the part of the buffer it filled.