Re: Fast way to read string one char at a time



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
.



Relevant Pages

  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: [OT] My First C# (warning - long post)
    ... cut me some slack - show me a COBOL program with less than 15 working-storage variables! ... Yes, the Trimis probably extra now, but that was my attempt to get it to quit griping at me that I had given it a "String", when it wanted a "char". ... public string IBreturn ...
    (comp.lang.cobol)