SIGN UP MEMBER LOGIN:    
ARTICLE

Iterations and Performance in .NET

Posted by Trevor Misfeldt Articles | Coding Best Practices April 15, 2003
I’ve been implementing numerical libraries in .NET and have come to some conclusions about iteration performance.
Reader Level:
Download Files:
 

Introduction

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

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

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. Thats not surprising as Im 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 theres no bounds checking. In summary, if you have large amounts of data and performance is critical, consider using managed C++.

Acknowledgement

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor