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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)
  • 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: 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)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)
  • extension_pack
    ... It is used to set upper loop -- limits for non-deterministic values thus avoiding the use of access -- types and enabling the functions to be used for synthesizeable code. ... DivisorVal: integer) return std_logic_vector; function "/"(DividendVal: string; DivisorVal: integer) return std_logic_vector; ... for loopVar in 0 to slvVal'length/4-1 loop ... end loop; if then return not resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to else return resultVar; -- "width mismatch" errors here are due to improper sizing of the vector that this function is assigned to end if; ...
    (comp.lang.vhdl)