Hi there,
After reading this topic I got interrested in making a little benchmark of this in .NET - to see how it performed.
Here is the source:
using System;using Microsoft.Win32;namespace ArrayBench{ delegate float TestMethod(); class Program { const int WIDTH = 1000; const int HEIGHT = 1000; long endTime; TestMethod[] tests; void start() { endTime = DateTime.Now.Ticks+100000000; } bool stop() { return DateTime.Now.Ticks>=endTime; } float linear() { int[] array = new int[WIDTH*HEIGHT]; int count = 0; int temp; start(); while(!stop()) { count++; for(int y=0; y<HEIGHT; y++) for(int x=0; x<WIDTH; x++) temp = array[y*WIDTH+x]; } return count; } float linearmatrix() { int[,] array = new int[WIDTH,HEIGHT]; int count = 0; int temp; start(); while(!stop()) { count++; for(int y=0; y<HEIGHT; y++) for(int x=0; x<WIDTH; x++) temp = array[x,y]; } return count; } float matrix() { int[][] array = new int[WIDTH][]; int count = 0; int temp; for(int i=0; i<WIDTH; i++) array = new int[HEIGHT]; start(); while(!stop()) { count++; for(int y=0; y<HEIGHT; y++) for(int x=0; x<WIDTH; x++) temp = array[x][y]; } return count; } public Program() { tests = new TestMethod[] { new TestMethod(linear), new TestMethod(linearmatrix), new TestMethod(matrix) }; } public void Run() { RegistryKey key = Registry.LocalMachine.OpenSubKey( "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); string name = (string)key.GetValue("Identifier"); int speed = (int)key.GetValue("~MHz"); Console.WriteLine("{0} at {1} MHz", name, speed); Console.WriteLine(); foreach(TestMethod test in tests) { Console.WriteLine("Testing {0}...", test.Method.Name); Console.WriteLine(" ... result: {0}", test()); Console.WriteLine(); } Console.WriteLine("Done!"); Console.ReadLine(); } [STAThread] static void Main(string[] args) { (new Program()).Run(); } }}
Every test generates an array of 1000x1000 ints, that makes 4000000 bytes. Then, the test tries to retreive the whole array as fast as possible within 10 seconds.
The first one checks for normal arrays, and uses the well-known method of storing the lines after each other.
The second one stores them in a [,] array.
The third is a normal multidimensional array, [][].
Here are the results on the school computers:
x86 Family 15 Model 2 Stepping 9 at 2394 MHz
Testing linear...
... result: 2792
Testing linearmatrix...
... result: 787
Testing matrix...
... result: 120
Done!
I don't have any binaries, sorry.
I never thought this would make such kind of difference - it really matters. the first method is almost 20 times as fast as the third one!
Good luck!