![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#11
| |||
| |||
|
|
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, |
|
but, as Brandon Hamm posted, knowing the toolset is important. |
#12
| |||
| |||
|
|
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 |
#13
| |||
| |||
|
|
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. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |