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
  #1  
Old   
 
Posts: n/a

Default Re: Fast way to read string one char at a time - 11-18-2005 , 08:49 AM






Hi,



The foreach loop will be slower in the case that you provide as it has a
much greater overhead for what your trying to achieve. If however, your
array/collection contained objects (just for arguments sake) and you were
required to do some additional processing on the object. The foreach loop
would start improving the performance (as well as maintainability by other
developers) as the variable would contain a reference to the actual object
allowing you to work on it directly. The for loop on the other hand would
require you to constantly reference an offset into the collection each time
you reference the object to modify a property or call a method.



You could reduce this overhead in the for loop for this example by having a
variable of the correct type and setting this in the first line of the for
statement. This will close the gape of the timings. There are still some
additional things that the foreach stament does to improve the stability of
your code that you would not be doing in the for loop.



The foreach will do the following:



First of all it extracts the IEnumerator interface from the collection using
GetEnumerator(). Then enters a while loop for the IEnumerator calling
MoveNext(). This will then typecast the current enumerator (Current() ) to
the Type specified by the variable then execute all your code.



This block is then wrapped in a try.finally block to ensure if anything goes
wrong it can free up the Enumerator interface if it implements the
IDisposable interface.

One thing you have to consider is the performance gain against the
maintainability as mentioned in the other posts. It's one thing to have
really optimised code but the price you pay comes when you or some one else
has to maintain it or modify it some months/years down the line. Having said
that a for loop is a basic operation that developers should have no problem
understanding. Are we talking about milliseconds increase in performance for
a cost of 30min + development time 4 months down the line trying to work out
how to modify it.

- Mike

---------------------------------------------------------------------------------
<a href="http://www.cogitar.net"> Cogitar Software. (www.cogitar.net) </a>
http://www.web-dominion.co.uk Web-Dominion. (Web Design and hosting )
---------------------------------------------------------------------------------

"Cosmin Prund" <cosminREMOVE (AT) adicomsft (DOT) NOSPAM.ro> wrote

Quote:
I'm trying to write a parser for a Interbase stored procedure and I want
to identify the fastest way to go through a string one char at a time.

As I'm really new to both c# and dotNet, I thought I'll share my findings
and ask for comments:

I found 4 different methods of going over the string one char at a time,
and my results seem somewhat unexpected!

[Method 1 - read directly from string - 100% timing]
for (int i=0;i<str_len;i++) c = TestString[i]


[Method 2 - read using StringBuilder - 200% timing]
for (int i=0;i<str_len;i++) c = TestBuilder[i]

[Method 3 - read using an CharEnumerator - 512% timing]
ch.Reset();
while (ch.MoveNext()) c = ch.Current;


[Method 4 - read using a foreach loop - 170% timing]
foreach (char c2 in TestString) c = c2

After doing all those tests I ended up with lots of questions:
1) Why is the "foreach" loop slower then the "for" loop?
2) Why is the CharEnumerator so slow? Am I using this class properly?
3) Is my test method fair? (see attachment for my code)





Reply With Quote
  #2  
Old   
Ahmed Fouad
 
Posts: n/a

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






I totally agree with Mike and Jon!

Always remember:

"Any fool can write programs which computer can understand, but the real
programmer is the one who write programs which other programmers can
understand!"


<Mike> wrote

Quote:
Hi,



The foreach loop will be slower in the case that you provide as it has a
much greater overhead for what your trying to achieve. If however, your
array/collection contained objects (just for arguments sake) and you were
required to do some additional processing on the object. The foreach loop
would start improving the performance (as well as maintainability by other
developers) as the variable would contain a reference to the actual object
allowing you to work on it directly. The for loop on the other hand would
require you to constantly reference an offset into the collection each
time you reference the object to modify a property or call a method.



You could reduce this overhead in the for loop for this example by having
a variable of the correct type and setting this in the first line of the
for statement. This will close the gape of the timings. There are still
some additional things that the foreach stament does to improve the
stability of your code that you would not be doing in the for loop.



The foreach will do the following:



First of all it extracts the IEnumerator interface from the collection
using GetEnumerator(). Then enters a while loop for the IEnumerator
calling MoveNext(). This will then typecast the current enumerator
(Current() ) to the Type specified by the variable then execute all your
code.



This block is then wrapped in a try.finally block to ensure if anything
goes wrong it can free up the Enumerator interface if it implements the
IDisposable interface.

One thing you have to consider is the performance gain against the
maintainability as mentioned in the other posts. It's one thing to have
really optimised code but the price you pay comes when you or some one
else has to maintain it or modify it some months/years down the line.
Having said that a for loop is a basic operation that developers should
have no problem understanding. Are we talking about milliseconds increase
in performance for a cost of 30min + development time 4 months down the
line trying to work out how to modify it.

- Mike

---------------------------------------------------------------------------------
a href="http://www.cogitar.net"> Cogitar Software. (www.cogitar.net)
/a
http://www.web-dominion.co.uk Web-Dominion. (Web Design and hosting )
---------------------------------------------------------------------------------

"Cosmin Prund" <cosminREMOVE (AT) adicomsft (DOT) NOSPAM.ro> wrote in message
news:uXrtQsp6FHA.3276 (AT) TK2MSFTNGP10 (DOT) phx.gbl...
I'm trying to write a parser for a Interbase stored procedure and I want
to identify the fastest way to go through a string one char at a time.

As I'm really new to both c# and dotNet, I thought I'll share my findings
and ask for comments:

I found 4 different methods of going over the string one char at a time,
and my results seem somewhat unexpected!

[Method 1 - read directly from string - 100% timing]
for (int i=0;i<str_len;i++) c = TestString[i]


[Method 2 - read using StringBuilder - 200% timing]
for (int i=0;i<str_len;i++) c = TestBuilder[i]

[Method 3 - read using an CharEnumerator - 512% timing]
ch.Reset();
while (ch.MoveNext()) c = ch.Current;


[Method 4 - read using a foreach loop - 170% timing]
foreach (char c2 in TestString) c = c2

After doing all those tests I ended up with lots of questions:
1) Why is the "foreach" loop slower then the "for" loop?
2) Why is the CharEnumerator so slow? Am I using this class properly?
3) Is my test method fair? (see attachment for my code)







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.