Is there a way to improve this:
private static int CountNewlines(string s)
{
int len = s.Length;
int c = 0;
for (int i=0; i < len; i++)
{
Well, String
implements IEnumerable
, so I'd definitely try:
s.Count( c => c == '\n' )
As nice as this looks, the original method is 30x faster :)
I haven't given up on the IEnumerable yet, so I've also tried:
int n = 0;
foreach( var c in s )
{
if ( c == '\n' ) n++;
}
return n;
which seems as fast as the original method.