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;
     }
   }

You might also like...

Comments

About the author

Trevor Misfeldt United States

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.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie