M
mp
Arne Vajhøj said:surprisingly(to me) your (elegant) for loop is winner hands down!
Often there is a trade off between speed and maintainability.
The Regex solutions are a lot easier to extend to add
more functionality.
The hand coded solution will soon be buried in an
unreadable mess of if statements.
if (s != ' ' || s[i - 1] != ' ')
{
res.Append(s);
}
now that's a thing of beauty
makes me laugh at my clumsy version
currentCharacter = inputString[CharPos];
if(currentCharacter ==' ')
{
if (preceedingCharacter != ' ')
returnString.Append(currentCharacter);
}
else
returnString.Append(currentCharacter);
preceedingCharacter = currentCharacter;
must be why your forloop is so much faster than mine
I would not expect that big a difference between those
two versions in speed. There are no big difference
in what they do - it is just how the code is written.
Arne
if i plug my version into your test framework there's not a big
difference...yours is a lttle faster
however in my test framework the regex solutions were faster than the loops
in yours, the loops were faster than the regex
some differences were my input strings were different and timing method a
little different
i'm trying to make a common test to compare apples to apples
one question in the meantime
in your test
public class WhileString : MultiSpaceTrimmerTest
{
public string Trim(string s)
{
string res = s;
int len;
do
{
len = res.Length;
res = res.Replace(" ", " ");
}
while (res.Length < len);
return res;
}
since string is immutable the line [res = res.Replace(" ", " ");]
creates a new string each time, right?
would it be "better" to use a stringBuilder in a case like this?
thanks
mark