HighTechTalks DotNet Forums  

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

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss RE: Fast way to read string one char at a time in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Fast way to read string one char at a time - 11-23-2005 , 12:48 PM






Cosmin Prund <cosminREMOVE (AT) adicomsft (DOT) NOSPAM.ro> wrote:
Quote:
I would personally prefer to investigate finding the most *readable*
ways of doing things - learning patterns rather than micro-optimisation
tips.

With all due respect Jon Skeet, you're turning my original question into
something it's not. My question was about raw speed and the raison different
implementations of what looks like the same thing are performing radically
different. I don't think anyone bases design decisions on
code-speed-profiling alone,
In my experience, people place far too much store on it. For instance,
you wrote:

<quote>
I will certainly not use the foreach loop on a string (in this case the
"for" loop can be just as easily implemented).
</quote>

The foreach loop is more readable, and so for the *vast* majority of
cases where the performance of the foreach part itself is irrelevant
you seem to have dismissed a more readable solution based on profiling
a no-op test case.

Quote:
but, as Brandon Hamm posted, knowing the toolset is important.
Just be aware that results can change radically when you're iterating
through different things - and between different framework versions.
(Iterating through a string using foreach was *much* slower in 1.0 than
in 1.1, for instance.)

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
  #12  
Old   
Phil Bolduc
 
Posts: n/a

Default Re: Fast way to read string one char at a time - 11-27-2005 , 05:00 PM






I recently came across Malcolm Crowe's http://cis.paisley.ac.uk/crow-ci0/,
Compiler tools in C#. It is a .NET implementation of a parser and tokenizer
generator similar to lex/yacc or bison/flex. It generates pretty fast
parsers/tokenizers. I like to let tools to help me a long freeing me from
the burden of writing too much low level or tedidious coding.

Phil Bolduc
Vancouver, BC Canada
-------------------------------------
Member of the .NetTiers team
http://cstemplates.sourceforge.net
-------------------------------------

"Cosmin Prund" wrote:

Quote:
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 (AT) pobox (DOT) com> wrote in message
news:MPG.1de6ed0b460045e298ca54 (AT) msnews (DOT) microsoft.com...
Cosmin Prund <cosminREMOVE (AT) adicomsft (DOT) NOSPAM.ro> 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 (AT) pobox (DOT) com
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




Reply With Quote
  #13  
Old   
Cosmin Prund
 
Posts: n/a

Default Re: Fast way to read string one char at a time - 12-01-2005 , 03:05 AM



Thanks for the tip, I gave CTools a try.
Unfortunately I came across some problems I'm not willing to fix myself:

1) (this is the most severe): I added a single "*" to a single regex in my
lex file and the test program that previously worked almost as expected is
now failing with a "NullReferenceException". While I'm sure I made something
wrong, I don't want to fight the tool trying to figure out WHY I'm getting
such an error. The TOOL is supposed to give me an error I can work with.

2) It keeps telling me that my lex file might produce an infinite stream of
tokens on invalid input. I don't like that either: The tool is supposed to
raise an error on error, not hang up in an infinite loop.

I'm not going to complain or send a bug-report to Phil Bolduc as he made it
very clear this is a tool design to aid in course, not a tool design to be
used in a production environment. I think he made a nice tool (it's the
first "lex/yacc" rewrite that I actually liked in a while) but it's not the
tool for me.

On the other hand:
Anyone knows any other good "compiler-compiler" tools for use with C# (or
Delphi, but I don't suppose I'll get an answer for that here :-) )?
I've found some references to something called vsCoco and "Visual Parser++"
using google. Of those two, I've got no idea what vsCoco does and I know for
sure "Visual Parser" is too expensive!

"Phil Bolduc" <Phil Bolduc (AT) discussions (DOT) microsoft.com> wrote

Quote:
I recently came across Malcolm Crowe's http://cis.paisley.ac.uk/crow-ci0/,
Compiler tools in C#. It is a .NET implementation of a parser and
tokenizer
generator similar to lex/yacc or bison/flex. It generates pretty fast
parsers/tokenizers. I like to let tools to help me a long freeing me from
the burden of writing too much low level or tedidious coding.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.