Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#

Iteration Performance in .NET

Rate me:
Please Sign up or sign in to vote.
4.69/5 (19 votes)
20 May 20031 min read 110.7K   567   25  
Article on the relative performance of various methods of iterating through large amounts of data.
using System;
using System.Collections;

using Iterations;

namespace Test
{
  class Test
  {
    private double d;
    private int reps;
    private int size;
    private double seconds;
    private Data data = null;
    private DateTime before;
    private TimeSpan total;

    [STAThread]
    static void Main(string[] args)
    {
      Test test;
      if ( args.Length > 1 ) 
      {
        test = new Test( int.Parse( args[0] ), int.Parse( args[1] ));
      } 
      else 
      {
        test = new Test();
      }
      test.TestAll();
    }

    public Test() : this( 1000, 1000000 )
    {
    }

    public Test( int reps, int size )
    {
      this.reps = reps;
      this.size = size;
      data = new Data( size );
    }

    private void TestAll()
    {
      System.Console.WriteLine();
      System.Console.WriteLine( "repetitions: " + reps );
      System.Console.WriteLine( "iterations: " + size.ToString( "e" ));
      System.Console.WriteLine();
      TestEnumeration();
      TestIndexing();
      TestIndirectArrays();
      TestDirectArrays();
      TestPointerMath();
      System.Console.WriteLine();
    }

    private void TestEnumeration() 
    {
      total = new TimeSpan( 0 );
      for ( int i = 0; i < reps; i++ ) 
      {
        before = DateTime.Now;
        DataEnumerator enumerator = data.GetEnumerator();
        while ( enumerator.MoveNext() ) 
        {
          d = enumerator.Current;
        }
        total += DateTime.Now - before;
      }
      seconds = total.Seconds + ( total.Milliseconds / 1000.0 );
      System.Console.WriteLine( "Enumeration: \t\t" + seconds + " seconds" );
    }

    private void TestIndexing()
    {
      total = new TimeSpan( 0 );
      for ( int i = 0; i < reps; i++ ) 
      {
        before = DateTime.Now;
        for ( int j = 0; j < size; j++ )
        {
          d = data[j];
        }
        total += DateTime.Now - before;
      }
      seconds = total.Seconds + ( total.Milliseconds / 1000.0 );
      System.Console.WriteLine( "Indexing: \t\t" + seconds + " seconds" );      
    }

    private void TestIndirectArrays()
    {
      total = new TimeSpan( 0 );
      for ( int i = 0; i < reps; i++ ) 
      {
        before = DateTime.Now;
        for ( int j = 0; j < size; j++ )
        {
          d = data.Array[j];
        }
        total += DateTime.Now - before;
      }
      seconds = total.Seconds + ( total.Milliseconds / 1000.0 );
      System.Console.WriteLine( "Indirect Arrays: \t" + seconds + " seconds" ); 
    }

    private void TestDirectArrays()
    {
      total = new TimeSpan( 0 );
      for ( int i = 0; i < reps; i++ ) 
      {
        before = DateTime.Now;
        double[] array = data.Array;
        for ( int j = 0; j < size; j++ )
        {
          d = array[j];
        }
        total += DateTime.Now - before;
      }
      seconds = total.Seconds + ( total.Milliseconds / 1000.0 );
      System.Console.WriteLine( "Direct Arrays: \t\t" + seconds + " seconds" );
    }

    private void TestPointerMath()
    {
      total = new TimeSpan( 0 );
      for ( int i = 0; i < reps; i++ ) 
      {
        before = DateTime.Now;
        Pointer.iterate( data );
        total += DateTime.Now - before;
      }
      seconds = total.Seconds + ( total.Milliseconds / 1000.0 );
      System.Console.WriteLine( "Pointer Math: \t\t" + seconds + " seconds" );
    }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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