-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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