Skip to content

Commit 0f7634d

Browse files
committed
added benchmarks
1 parent e1b850d commit 0f7634d

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

benchmark/Benchmark.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ namespace SimdUnicodeBenchmarks
1515
public class Checker
1616
{
1717
List<char[]> names;
18-
List<bool> results;
19-
public static bool RuntimeIsAsciiApproach(ReadOnlySpan<char> s)
20-
{
21-
// The runtime as of NET 8.0 has a dedicated method for this, but
22-
// it is not available prior to that, so let us branch.
18+
List<bool> results;
19+
20+
public static bool RuntimeIsAsciiApproach(ReadOnlySpan<char> s)
21+
{
22+
// The runtime as of NET 8.0 has a dedicated method for this, but
23+
// it is not available prior to that, so let us branch.
2324
#if NET8_0_OR_GREATER
2425
return Ascii.IsValid(s);
2526
#else
@@ -99,6 +100,36 @@ public void RuntimeIsAscii()
99100
}
100101
}
101102

103+
104+
[Benchmark]
105+
public void TestErrorGetIndexOfFirstNonAsciiByteBenchmark()
106+
{
107+
foreach (char[] chars in names)
108+
{
109+
byte[] ascii = Encoding.UTF8.GetBytes(chars);
110+
111+
for (int i = 0; i < ascii.Length; i++)
112+
{
113+
ascii[i] += 0b10000000;
114+
115+
unsafe
116+
{
117+
fixed (byte* pAscii = ascii)
118+
{
119+
nuint result = Ascii.GetIndexOfFirstNonAsciiByte(pAscii, (nuint)ascii.Length);
120+
if (result != (nuint)i)
121+
{
122+
throw new Exception($"Expected non-ASCII character at index {i}, but found at index {result}");
123+
}
124+
}
125+
}
126+
127+
ascii[i] -= 0b10000000;
128+
}
129+
}
130+
}
131+
132+
102133
}
103134

104135
public class Program

0 commit comments

Comments
 (0)