Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!natinst!sequoia!memqa!r91400 From: r91400@memqa.uucp (Michael C. Grant) Newsgroups: comp.lang.c++ Subject: Re: parameter[][] Message-ID: <5044@memqa.uucp> Date: 8 Aug 90 10:27:17 GMT References: <1545@m1.cs.man.ac.uk> <26B9BF5A.7291@orion.oac.uci.edu> <37961@ucbvax.BERKELEY.EDU> <65983@lll-winken.LLNL.GOV> Organization: Memory R&QA, Motorola SPD Lines: 35 In article <65983@lll-winken.LLNL.GOV>, jac@gandalf..llnl.gov (James Crotinger) writes: > In article <37961@ucbvax.BERKELEY.EDU> jbuck@galileo.berkeley.edu (Joe Buck) writes: >> >> >>double **mat_alloc (int nrows, int ncols) { >> double **t = new double* [nrows]; >> for (int i = 0; i < ncols; i++) >> t[i] = new double [ncols]; >>} >> > There are at least a couple of problems with this approach. The > first is that, as written, the inter-row spacing is not constant. This > may hurt your performance on some architectures when you try to do a > loop over the first index. (This is particularly true of vectorizing > architectures but can also give a performance hit on systems with > interleaved memory access). In fact optimization in general is > greatly hindered by the use of a double ** to represent a simple > square or rectanglar array since the optimizer no longer has any way > of knowing that it is, indeed, dealing with such a simple layout of > the data. It really depends on how you access the data. If you are performing sequential access, then yes I agree that a simple rectangular array would speed things up quite a bit--but for relatively random access, you really can't beat this double ** approach. Some processors can implement this as a single indirect addressing with offset instruction. Most could do it in two instructions. But, the simple rectangular array would require a multiply and addition IN addition to the access. Of course, if access time is only a small portion of your program's execution time, then optimizing access won't to diddley to improve the performance. For most programs, it's worthless to quibble over it (see Amdahl's law...) Michael C. Grant