On Wed, 20 Mar 2002, Gordon Pearce wrote: > > > > 2 possible implementations of this are: > > > > [deletia] > > > > > The first solution will be faster but the algorithm hasn't changed. It's > > Why will it be faster? Is it something to do with the way the array is > referenced? > > ( /me isn't great at C at all) >
It's to do with the way that multi-dimensional arrays are laid out in memory. The second version strode through memory, back and forth, back and forth. The first version accesses each element in a row in turn, which means walking sequentially through memory. The second will produce awful cache performance in relation to the first. If you want to read a better explaination search for terms like "row major order". -- Nicholas Wolverson -------------------------------------------------------------------- http://www.lug.org.uk http://www.linuxportal.co.uk http://www.linuxjob.co.uk http://www.linuxshop.co.uk --------------------------------------------------------------------
