Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Forked from aarondandy/netcore linecount
Last active January 21, 2020 15:04
Show Gist options
  • Save Buildstarted/e48a2136a48dcd85e25caa06a4dd2ffb to your computer and use it in GitHub Desktop.
Save Buildstarted/e48a2136a48dcd85e25caa06a4dd2ffb to your computer and use it in GitHub Desktop.
netcore 3.1. hardcoded to the location of a 1.6gb text file
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.IO;
using System.Text;
namespace BensWordCounter
{
public class Program
{
public const string FilePath = @"C:\tools\hashcat-5.1.0\rockyou.txt";
private Stream FileStream;
private byte[] FileData;
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Program>();
}
[GlobalSetup]
public void GlobalSetup()
{
FileData = File.ReadAllBytes(FilePath);
}
[IterationSetup]
public void Setup()
{
FileStream = new MemoryStream(FileData);
}
[Params(1024, 4096, 40960, 1024000)]
public int BufferSize;
[Benchmark]
public int AaronsSimpleVersion()
{
int count = 0;
var buffer = new Span<char>(new char[BufferSize]);
using var reader = new StreamReader(FileStream, Encoding.UTF8, true, bufferSize: buffer.Length);
int readLength;
int sliceIndex;
while ((readLength = reader.Read(buffer)) != 0)
{
var slice = buffer.Slice(0, readLength);
while ((sliceIndex = slice.IndexOf('\n')) >= 0)
{
slice = slice.Slice(sliceIndex + 1);
count++;
}
}
return count;
}
[Benchmark]
public int BensNotSoSimpleVersion()
{
const int slicesize = 32;
var count = 0;
Span<byte> buffer = stackalloc byte[BufferSize];
using var fileStream = FileStream;
var mask = new Vector<byte>((byte)'\n');
int readLength;
while ((readLength = fileStream.Read(buffer)) != 0)
{
var slice = buffer.Slice(0, readLength);
while (slice.Length > 0)
{
var sub = slice.Slice(0, Math.Min(slice.Length, slicesize));
if (sub.Length == slicesize)
{
var vector = new Vector<byte>(sub);
var equal = Vector.Equals(vector, mask);
var negation = Vector.Negate(equal);
var subcount = Vector.Dot(negation, Vector<byte>.One);
count += subcount;
slice = slice.Slice(slicesize);
}
else
{
Span<byte> sub2 = stackalloc byte[slicesize];
sub.TryCopyTo(sub2);
var vector = new Vector<byte>(sub2);
var equal = Vector.Equals(vector, mask);
var negation = Vector.Negate(equal);
var subcount = Vector.Dot(negation, Vector<byte>.One);
count += subcount;
break;
}
}
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment