12,957,007 members (56,584 online)
alternative version

#### Stats

97K views
25 bookmarked
Posted 30 Apr 2003

# Iteration Performance in .NET

, 20 May 2003
 Rate this:
Article on the relative performance of various methods of iterating through large amounts of data.

## 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 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
{
get
{
return internal_.Array[index_];
}
}

object IEnumerator.Current
{
get
{
return Current;
}
}

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. <PRE lang=mc++> 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:

<PRE lang=mc++> 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...

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

### Acknowledgements

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.

A list of licenses authors might use can be found here

## Share

 CEO CenterSpace Software 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.

## You may also be interested in...

 Pro Pro

 FirstPrev Next
 Re: Enumerations asm4328-May-03 11:21 asm43 28-May-03 11:21
 Update Trevor Misfeldt7-May-03 5:55 Trevor Misfeldt 7-May-03 5:55
 Re: Update dog_spawn21-May-03 16:22 dog_spawn 21-May-03 16:22
 Re: Update Morten Sparding11-Mar-05 22:28 Morten Sparding 11-Mar-05 22:28
 unsafe C# John Rayner7-May-03 5:04 John Rayner 7-May-03 5:04
 Re: unsafe C# Trevor Misfeldt7-May-03 5:53 Trevor Misfeldt 7-May-03 5:53
 Enumeration performance James T. Johnson2-May-03 1:59 James T. Johnson 2-May-03 1:59
 Re: Enumeration performance Trevor Misfeldt2-May-03 4:29 Trevor Misfeldt 2-May-03 4:29
 Re: Enumeration performance Andy Gray6-May-03 6:23 Andy Gray 6-May-03 6:23
 Re: Enumeration performance JedrekM20-Jun-03 8:35 JedrekM 20-Jun-03 8:35
 Re: Enumeration performance Trevor Misfeldt20-Jun-03 9:46 Trevor Misfeldt 20-Jun-03 9:46
 Re: Enumeration performance Oscar Bowyer6-May-03 3:44 Oscar Bowyer 6-May-03 3:44
 Re: Enumeration performance James T. Johnson6-May-03 4:36 James T. Johnson 6-May-03 4:36
 Oscar Bowyer wrote: Any info on when and how generics will be implemented? There was a powerpoint presentation from some conference a while back, but I can't remember where I saw it. But this might be made obsolete by some info below. IIRC the syntax was similar to C++, but it added more to it. Such as letting the template writer specify that the template parameter implement a certain interface (such as `IComparable` for a template which does sorting). Oscar Bowyer wrote: I've read that they will be "similar" to C++ templates, and not available in VB.NET. I've seen the same. I guess you could call the research group at MS[^] the authority, and they have a new (May 1, 2003) release for the SSCLI[^] which adds generics to it. Also note the PDF file they released back in May of 2001 (while the framework was still in beta 2, IIRC). James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation
 Re: Enumeration performance mikasa22-May-03 3:11 mikasa 22-May-03 3:11
 Re: Enumeration performance James T. Johnson22-May-03 9:11 James T. Johnson 22-May-03 9:11
 Re: Enumeration performance mikasa22-May-03 9:18 mikasa 22-May-03 9:18
 Re: Enumeration performance James T. Johnson22-May-03 9:29 James T. Johnson 22-May-03 9:29
 Last Visit: 31-Dec-99 18:00     Last Update: 29-May-17 23:12 Refresh 1