Click here to Skip to main content
Click here to Skip to main content

Iteration Performance in .NET

, 20 May 2003
Rate this:
Please Sign up or sign in to vote.
Article on the relative performance of various methods of iterating through large amounts of data.


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 DataEnumerator GetEnumerator() 
      return new DataEnumerator( this );

    IEnumerator IEnumerable.GetEnumerator()
      return GetEnumerator();
  public struct DataEnumerator : IEnumerator
    private Data internal_;
    private int index_;

    public DataEnumerator( Data data ) 
      internal_ = data;
      index_ = 1;

    public double Current
        return internal_.Array[index_];

    object IEnumerator.Current 
        return Current;
    public bool MoveNext()
      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]
        return array_[position];

Method #3: Indirect Array

I created a property to access the array.

    public double[] Array
        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;

I called it this way:

        Pointer.iterate( data );


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


Enumeration is always slow. That’s not surprising as I’m using a general data structure to hold the doubles. 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++.


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Trevor Misfeldt
CEO CenterSpace Software
United States United States
Trevor has held demanding development positions for a variety of firms using C++, Java, .NET, and other technologies, including Rogue Wave Software, CleverSet, and ProWorks. He is coauthor of The Elements of Java Style , The Elements of C++ Style, and The Elements of C# Style, published by Cambridge University Press. 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.

Comments and Discussions

GeneralNice PinmemberAnthony_Yio13-Oct-03 16:51 
GeneralEnumerations PinmemberPaul Evans27-May-03 0:19 
GeneralRe: Enumerations Pinsussasm4328-May-03 12:21 
GeneralUpdate PinmemberTrevor Misfeldt7-May-03 6:55 
GeneralRe: Update Pinmemberdog_spawn21-May-03 17:22 
GeneralRe: Update PinsussMorten Sparding11-Mar-05 23:28 
Generalunsafe C# PinmemberJohn Rayner7-May-03 6:04 
GeneralRe: unsafe C# PinmemberTrevor Misfeldt7-May-03 6:53 
GeneralEnumeration performance PinmemberJames T. Johnson2-May-03 2:59 
GeneralRe: Enumeration performance PinsussTrevor Misfeldt2-May-03 5:29 
GeneralRe: Enumeration performance PinmemberAndy Gray6-May-03 7:23 
GeneralRe: Enumeration performance PinmemberJedrekM20-Jun-03 9:35 
My times are quite different (.NET 2003, Framework 1.1, Athlon 1900+, 512MB RAM):
repetitions: 1000
iterations: 1,000000e+006
Enumeration: 17,103 seconds
Indexing: 11,886 seconds
Indirect Arrays: 11,876 seconds
Direct Arrays: 11,275 seconds
Pointer Math: 3,394 seconds
And my little trick:
- add member in DataEnumerator:
private int len_; (I would use m_nLen, but it's a detail Wink | ;-)
- add initialization of above member in DataEnumerator constructor:
len_ = internal_.Array.Length
- change some line in DataEnumerator::MoveNext to:
if (index_ >= len_)
Times after changes:
repetitions: 1000
iterations: 1,000000e+006
Enumeration: 15,781 seconds
Indexing: 11,636 seconds
Indirect Arrays: 11,626 seconds
Direct Arrays: 11,696 seconds
Pointer Math: 2,373 seconds
Enumeration test is little slower than others.
IMHO it is not worth to write ugly code to be little faster.
So: use enumerations to have pretty code and use PointerMath method if you use some advanced math algorithms.

GeneralRe: Enumeration performance PinmemberTrevor Misfeldt20-Jun-03 10:46 
GeneralRe: Enumeration performance PinmemberOscar Bowyer6-May-03 4:44 
GeneralRe: Enumeration performance PinmemberJames T. Johnson6-May-03 5:36 
GeneralRe: Enumeration performance Pinmembermikasa22-May-03 4:11 
GeneralRe: Enumeration performance PinmemberJames T. Johnson22-May-03 10:11 
GeneralRe: Enumeration performance Pinmembermikasa22-May-03 10:18 
GeneralRe: Enumeration performance PinmemberJames T. Johnson22-May-03 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411022.1 | Last Updated 21 May 2003
Article Copyright 2003 by Trevor Misfeldt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid