Skip to content

Instantly share code, notes, and snippets.

@halter73
Created December 17, 2015 01:32
Show Gist options
  • Save halter73/236f9f300b7f5cfe1d40 to your computer and use it in GitHub Desktop.
Save halter73/236f9f300b7f5cfe1d40 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Numerics;
namespace ConsoleApp1
{
public class Program
{
public void Main(string[] args)
{
var vectorArray = new Vector<byte>[Vector<byte>.Count];
for (int i = 0; i < Vector<byte>.Count; i++)
{
var byteArray = new byte[Vector<byte>.Count];
byteArray[i] = 1;
vectorArray[i] = new Vector<byte>(byteArray);
}
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 10000000; i++)
{
for (int j = 0; j < Vector<byte>.Count; j++)
{
FindFirstEqualByte2(vectorArray[j]);
}
}
stopWatch.Stop();
Console.WriteLine(stopWatch.ElapsedMilliseconds.ToString());
}
private static int FindFirstEqualByte(Vector<byte> chEquals)
{
// Quasi-tree search
var vector64 = Vector.AsVectorInt64(chEquals);
for (var i = 0; i < Vector<long>.Count; i++)
{
var longValue = vector64[i];
if (longValue == 0) continue;
var shift = i << 1;
var offset = shift << 2;
var vector32 = Vector.AsVectorInt32(chEquals);
if (vector32[shift] != 0)
{
if (chEquals[offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
return ++offset;
}
offset += 4;
if (chEquals[offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
return ++offset;
}
throw new InvalidOperationException();
}
private static int FindFirstEqualByte2(Vector<byte> chEquals)
{
var vector64 = Vector.AsVectorInt64(chEquals);
for (var i = 0; i < Vector<long>.Count; i++)
{
var longValue = vector64[i];
if (longValue == 0) continue;
var offset = i << 3;
if (chEquals[offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
if (chEquals[++offset] != 0) return offset;
return ++offset;
}
throw new InvalidOperationException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment