Community Experience With Iterators vs Overwriting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jehugaleahsa@gmail.com

    Community Experience With Iterators vs Overwriting

    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.

    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>.

    Does anyone have a good explanation for why this is true? Is it just a
    fluke on my part? And what does the community say about using one over
    the other? I honestly think there is something beautiful about
    Iterators and would like to use them more often than not.

    Thanks for your input,
    Travis
  • Michael Starberg

    #2
    Re: Community Experience With Iterators vs Overwriting


    <jehugaleahsa@g mail.comwrote in message
    news:e27350c5-6199-412b-9f90-032f2b782e93@h1 1g2000prf.googl egroups.com...
    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.whateve r
    == 'something');

    ... that is just plain ugly!

    foreach is your friend!
    Travis
    Happy Coding
    - Michael Starberg


    Comment

    Working...