Performing Various Iteration Methods in .NET

Environment: .NET

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

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 #4: Direct Array

I created a reference to the array.

        double[] array = data.Array;

Then, I iterate through that reference.

          d = array[j];

Method #5: 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 );

Conclusions

To test the different methods, I allocated 1,000,000 doubles into an array and indexed over all of them. I repeated this 1,000 times to minimize randomness. Here are the results...



Click here for a larger image.

Enumeration is always slow. That's not surprising because I'm using a general data structure to hold the doubles. Each access performs a cast. The three operator/property methods differed very slightly. These are probably all optimized similarly. Using pointer math to traverse over the raw data was significantly faster. This is probably due to the fact that there's no bounds checking.

In summary, if you have large amounts of data and performance is critical, consider using managed C++.

Acknowledgements

Thanks to Mark Vulfson of ProWorks for tips on using the Flipper Graph Control. Also, to my colleagues Ken Baldwin and Steve Sneller at CenterSpace Software.

About the Author

Trevor Misfeldt is the co-founder and CEO of CenterSpace Software, which specializes in .NET numerical method libraries. Trevor has worked as a software engineer for eight years. He has held demanding positions for a variety of firms using C++, Java, .NET, and other technologies, including Rogue Wave Software, CleverSet Inc., and ProWorks. He is coauthor of Elements of Java Style, published by Cambridge University Press, and is currently working on a follow-up book for C++. He has also served on a course advisory board of the University of Washington. His teams have won the JavaWorld "GUI Product of the Year" and XML Magazine "Product of the Year" awards. Trevor holds a BSc in Computer Science from the University of British Columbia and a BA in Economics from the University of Western Ontario.

Downloads

Download source - 6 Kb


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Mobile is introducing sweeping changes throughout your workplace. As a senior stakeholder driving mobile initiatives in your organization, you may be lost in a sea of technologies and claims from vendors promising rapid delivery of applications to your employees, customers, and partners. To help explain some of the topics you will need to be aware of, and to separate the must-haves from the nice-to-haves, this reference guide can help you with applying a mobile strategy in the context of application …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds