Library tutorials & articles

Iteration Methods

Introduction

Introduction

I’ve been implementing numerical libraries in .NET and have come to some conclusions about iteration performance. My classes have to hold a large amount of data and be able to iterate through that data as quickly as possible. In order to compare various methods, I created a simple class called Data that encapsulates an array of doubles.

Method #1: Enumeration

Data implements IEnumerable. It contains GetEnumerator which returns its own DataEnumerator, an inner class.


  public IEnumerator GetEnumerator()
   {
     return new DataEnumerator( this );
   }
   
   internal class DataEnumerator : IEnumerator
   {
     private Data internal_ = null;
     private int index = -1;

     public DataEnumerator( Data data )
     {
       internal_ = data;
     }

     public object Current
     {
       get
       {
         return internal_.Array[index];
       }
     }

     public bool MoveNext()
     {
       index++;
       if ( index >= internal_.Array.Length )
       {
         return false;
       }
       return true;
     }

     public void Reset()
     {
       index = -1;
     }
   }

Comments

  1. 06 Aug 2003 at 14:17


    Thanks for the feedback!  I tried putting in the extra step.  The results are virtually unchanged from previous changes.  Please try it out yourself.


    Cheers,
    Trevor



    Trevor Misfeldt
    CEO, CenterSpace Software

  2. 13 May 2003 at 15:47

    You are merely accessing the reference to where the data is kept for the array, but are never actually accessing the data, which you do elsewhere (for example Method #2). If you were to actually access the data, then I think your method would perform similar to Method #2. I don't think that the actual data is read until you access the variable. So perhaps to complete the test, I would perform a d += 1 in each loop to make sure that the data is accessed by each method your base class in which the object exists.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Iteration Methods.

Leave a comment

Sign in or Join us (it's free).

Trevor Misfeldt CEO of CenterSpace Software. We make numerical analysis class libraries for the .NET platform. Author of Elements of Java Style and Elements of C++ Style.
AddThis

Related discussion

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Events coming up

  • Aug 28

    St. Louis Day of .NET

    St. Charles, United States

    Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend

We'd love to hear what you think! Submit ideas or give us feedback