Re: Fast way to read string one char at a time
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Fri, 18 Nov 2005 17:43:26 -0000
Cosmin Prund <cosminREMOVE@xxxxxxxxxxxxxxxxxxx> wrote:
> The whole "application" I'm working on is an _exercise_ to learn dotNET and
> c#. As part of this exercise I wanted to find the fastest way to go over a
> string one char at a time. As all of my benchmarking suggested, the fastest
> way to do this is to use the "for" loop, not the "foreach" loop nor any
> other method. As I've sad, this has been a valuable learning experience for
> me, as I now know how the "foreach" loop is implemented and I've got a
> fairly good idea as to why the "for" loop is fast.
Well, bear in mind that that may well change in the future -
performance is a bit like that.
You should also bear in mind that very, very few situations will
actually notice *any* difference between the two in terms of
performance, whereas the difference in readability can be quite
significant.
> Moving on toward my real goal (writing the "tokenizer" part of my parser),
> the "foreach" loop is pretty much unusable because I'm not really going to
> go over the string in one go - I'll go over part of the string, return a
> "token" and wait till the parser request one more token.
In that case, you're not really doing what your subject implies. That's
okay, but it's worth noting because suddenly the readability criteria
change.
> What would _really_
> help would be identifying the _fastest_ way to read those chars out of a
> string given an char index. As a matter of fact it doesn't have to be a
> "char" and "string" method - I'm willing to work with char arrays, byte
> arrays, anything that would improve performance.
Bear in mind that as soon as you change data type, you may find that
foreach is now faster. One thing you almost *certainly* will find if
you use arrays is that:
int len = array.Length;
for (int i=0; i < len; i++)
{
DoSomethingWith(array[i]);
}
is slower than
for (int i=0; i < array.Length; i++)
{
DoSomethingWith(array[i]);
}
Or at least, it was last time I checked...
However, to go back to my main theme - your willingness to completely
change the way you work for the sake of *performance* is worrying - at
least unless you've actually proved you've got a performance problem.
Have you? Are things really working too slowly for you, in a real
situation with real data, running the release build? If not, you
shouldn't be worrying about the performance yet, and you certainly
shouldn't be sacrificing elegance and readability for the sake of
performance.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- References:
- Re: Fast way to read string one char at a time
- From: Cosmin Prund
- Re: Fast way to read string one char at a time
- From: Jon Skeet [C# MVP]
- Re: Fast way to read string one char at a time
- From: Cosmin Prund
- Re: Fast way to read string one char at a time
- Prev by Date: Re: Fast way to read string one char at a time
- Next by Date: .NET CF Drawing Problems
- Previous by thread: Re: Fast way to read string one char at a time
- Next by thread: Re: Fast way to read string one char at a time
- Index(es):
Relevant Pages
|