Re: Fast way to read string one char at a time
- From: "Cosmin Prund" <cosminREMOVE@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 18 Nov 2005 14:00:00 +0200
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.
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. 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.
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1de6ed0b460045e298ca54@xxxxxxxxxxxxxxxxxxxxxxx
> Cosmin Prund <cosminREMOVE@xxxxxxxxxxxxxxxxxxx> wrote:
>> Thanks Ivan, I'm learning something every day.
>>
>> It did not occur to me that the c# compiler will use a generic method for
>> implementing the foreach loop on a string, I somehow expected it to be
>> implemented using some compiler magic. I will certainly not use the
>> foreach
>> loop on a string (in this case the "for" loop can be just as easily
>> implemented).
>
> Just as easily, but IMO not as readably. If this is something which is
> going to involve database work, how sure are you that the looping
> itself is the bottleneck?
>
> I note that you're taking the string length first and putting it in a
> separate variable for your "for" loop by the way rather than using
> TestString.Length each time - have you evaluated the performance hit of
> that? Certainly for array access, it would actually be faster to do it
> the more readable way (without the extra variable).
>
> I can only repeat: unless you are *sure* you've found a bottleneck in
> your code, go for the most readable version.
>
> --
> 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
.
- Follow-Ups:
- Re: Fast way to read string one char at a time
- From: Phil Bolduc
- Re: Fast way to read string one char at a time
- From: Brandon Hamm
- 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
- 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
- Prev by Date: Re: Fast way to read string one char at a time
- Next by Date: Re: Fast way to read string one char at a time
- 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
|