Iteration Methods

More Methods

Method #2: Indexing

I implemented an index operator on the class which simply calls the index operator on the array.

public double this[int position]
{
 get
 {
   return array_[position];
 }
}

Method #3: Indirect Array

I created a property to access the array.

public double[] Array
{
  get
  {
    return array_;
  }
}

When iterating, I called the Array property and then its index operator.

d = data.Array[j];

Method #3: Direct Array

I created a reference to the array.

double[] array = data.Array;

Then, I iterate through that reference.

d = array[j];

Method #4: Pointer Math

Finally, I tried improving performance by iterating through the array in Managed C++ using pointer manipulation.

static void iterate( Data& data )
{
 double d;
 double __pin* ptr = &( data.Array[0] );
 for ( int i = 0; i < data.Array.Length; i++ )
 {
   d = *ptr;
   ++ptr;
 }
}

I called it this way:

Pointer.iterate( data );

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.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay