I have a simple question. I'm working with a gigantic string in C# and I'm repeatedly going through it and accessing individual characters via the [] operator. Is it faster to turn the string into a char[] and use the [] operator on the char array or is my approach faster? Or are they both the same? I want my program to be bleeding edge fast.
If you need the absolute fastest implementation, then you can drop to unsafe and use pointers:
string s = ...
int len = s.Length;
fixed (char* ptr = s)
{
// talk to ptr[0] etc; DO NOT go outside of ptr[0] <---> ptr[len-1]
}
that then avoids range checking but:
unsafeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With