Re: Community Experience With Iterators vs Overwriting

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




<jehugaleahsa@xxxxxxxxx> wrote in message
news:e27350c5-6199-412b-9f90-032f2b782e93@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello:

I was running some tests today and found an interesting result. I have
a large library where half the methods work on IEnumerable<T>
(Iterators similar to LINQ), and another that works on List<T>, using
indices to overwrite values.

ok.


I wanted to see which performed better overall (I guessed wrong). What
surprised me what that a well-written Iterator typically ran faster
(usually generating a new IEnumerable<U>) than a similar method that
just overwrites a List<T>.

Good for you.
But what is 'faster'?

Is performance a problem for you?


Does anyone have a good explanation for why this is true?

I have not.

Is it just a fluke on my part? And what does the community say about using
one over
the other?

I say use 'foreach' over 'for'.
Not because one is faster or not,
but just to get rid of the
'i <= list.Length' bugs.

Also it looks nicer. Much more readable.
Also with typing foreach and press tab, you can do a iterator very quickly.

With the common snippet over foreach, with the old snippet started with
'var',
I just had to write my own foreach-snippet, 'fe' that skips that first step.
Don't need to tab through 'var' with my snippet. 'var' is just fine for me
=)


I honestly think there is something beautiful about
Iterators and would like to use them more often than not.

I honsestly also think they are beautiful. May have to do something with
that I actaully have to maintain other peoples code!? =)
The for(init; expression; iter) is HighTech from the -70ies (60ies?). - Move
along folks, there is nothing to see here.. move along...


Thanks for your input,

Use foreach whenever you can.
And Never, Never and NEVER, do a myList.ForEach(lambda => lambda.whatever
== 'something');

... that is just plain ugly!

foreach is your friend!

Travis

Happy Coding
- Michael Starberg


.



Relevant Pages

  • Re: My silliest extention made yet 2008...
    ... to define my own delegate type again for a long time:) ... predicate and func delegates. ... foreach loop, but it doesn't look quite as neat as: ... you've expanded a snippet to longer code, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PHP] RecursiveArrayIterator
    ... well garbage collection will remove the copy of an array created by foreach, ... Foreach doesn't bother creating a copy of the array either. ... about arrays and if you're using iterators then you're also talking ...
    (php.general)
  • newbie: foreach
    ... I'm sure the below can be simplified with some sort of "foreach", ... Snippet of code attached: ... # Display prompt; push the partial line of text to the console ...
    (comp.lang.tcl)
  • Re: What do you think about the for-each loop?
    ... And you could do a similar thing for reading lines with BufferedReader. ... So this would really be a request for a library enhancement. ... The fact that foreach requires an object which implements Iterable means that only one of these views can be iterated over with foreach, and the other views have to be iterated using the old style. ... I suppose this could also be fixed with a library change, by making Iterators implement Iterable, but that would be nasty. ...
    (comp.lang.java.programmer)
  • Re: foreach for Prolog
    ... is a built-in named foreach for describing loops. ... iterators' outputs, so I assume this is logical ... makes it clear binding can't occur in those cases.] ...
    (comp.lang.prolog)