@@ -15,17 +15,19 @@ namespace SimdUnicodeBenchmarks
15
15
public class Checker
16
16
{
17
17
List < char [ ] > names ;
18
- List < char [ ] > nonAsciichars ;
18
+ List < char [ ] > nonAsciichars ;
19
19
public List < byte [ ] > nonAsciiByteArrays ; // Declare at the class level
20
20
21
21
List < bool > results ;
22
22
23
23
public static bool RuntimeIsAsciiApproach ( ReadOnlySpan < char > s )
24
24
{
25
+
25
26
// The runtime as of NET 8.0 has a dedicated method for this, but
26
27
// it is not available prior to that, so let us branch.
27
28
#if NET8_0_OR_GREATER
28
- return Ascii . IsValid ( s ) ;
29
+ return System . Text . Ascii . IsValid ( s ) ;
30
+
29
31
#else
30
32
foreach ( char c in s )
31
33
{
@@ -38,6 +40,8 @@ public static bool RuntimeIsAsciiApproach(ReadOnlySpan<char> s)
38
40
return true ;
39
41
#endif
40
42
}
43
+
44
+
41
45
public static char [ ] GetRandomASCIIString ( uint n )
42
46
{
43
47
var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ01234567é89" ;
@@ -56,8 +60,8 @@ public static char[] GetRandomASCIIString(uint n)
56
60
public static char [ ] GetRandomNonASCIIString ( uint n )
57
61
{
58
62
// Chose a few Latin Extended-A and Latin Extended-B characters alongside ASCII chars
59
- var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ01234567é89šžŸũŭůűųŷŹźŻżŽ" ;
60
-
63
+ var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ01234567é89šžŸũŭůűųŷŹźŻżŽ" ;
64
+
61
65
var chars = new char [ n ] ;
62
66
var rd = new Random ( 12345 ) ; // fixed seed
63
67
@@ -133,14 +137,26 @@ public void TestErrorGetIndexOfFirstNonAsciiByteBenchmark()
133
137
{
134
138
fixed ( byte * pNonAscii = nonAsciiBytes )
135
139
{
136
- nuint result = Ascii . GetIndexOfFirstNonAsciiByte ( pNonAscii , ( nuint ) nonAsciiBytes . Length ) ;
140
+ nuint result = SimdUnicode . Ascii . GetIndexOfFirstNonAsciiByte ( pNonAscii , ( nuint ) nonAsciiBytes . Length ) ;
137
141
}
138
142
}
139
143
}
140
144
}
141
145
142
-
143
-
146
+ [ Benchmark ]
147
+ public void RuntimeGetIndexOfFirstNonAsciiByteBenchmark ( )
148
+ {
149
+ foreach ( byte [ ] nonAsciiBytes in nonAsciiByteArrays ) // Use nonAsciiByteArrays directly
150
+ {
151
+ unsafe
152
+ {
153
+ fixed ( byte * pNonAscii = nonAsciiBytes )
154
+ {
155
+ nuint result = Competition . Ascii . GetIndexOfFirstNonAsciiByte ( pNonAscii , ( nuint ) nonAsciiBytes . Length ) ;
156
+ }
157
+ }
158
+ }
159
+ }
144
160
}
145
161
146
162
public class Program
0 commit comments