From: bart.c on 28 Apr 2010 13:47 "Seebs" <usenet-nospam(a)seebs.net> wrote in message news:slrnhtgquu.lhj.usenet-nospam(a)guild.seebs.net... > On 2010-04-28, Richard Heathfield <rjh(a)see.sig.invalid> wrote: >>> { >>> for(size_t xInd = 0; xInd < data.size(); ++xInd) >>> for(size_t yInd = 0; yInd < data[xInd].size(); ++yInd) >>> for(size_t zInd = 0; zInd < data[xInd][yInd].size(); ++zInd) >>> { >>> if(data[xInd][yInd][zInd] == value) >>> return &data[xInd][yInd][zInd]; >>> } >>> >>> return 0; >>> } > I put it to you that it is impossible to write the above loop with > remotely > comparable clarity without using a jump of some sort out of the inner > loop. Any attempt to do so will result in a MASSIVE loss of clarity. int* findvalue(int value) {int *a=0; for(int xInd = 0; !a && xInd < datasize; ++xInd) for(int yInd = 0; !a && yInd < dataxindsize; ++yInd) for(int zInd = 0; !a && zInd < dataxindyindsize; ++zInd) { if(data[xInd][yInd][zInd] == value) a=&data[xInd][yInd][zInd]; } return a; } -- Bartc
From: Seebs on 28 Apr 2010 14:16 On 2010-04-28, bart.c <bartc(a)freeuk.com> wrote: > int* findvalue(int value) > {int *a=0; > > for(int xInd = 0; !a && xInd < datasize; ++xInd) > for(int yInd = 0; !a && yInd < dataxindsize; ++yInd) > for(int zInd = 0; !a && zInd < dataxindyindsize; ++zInd) > { > if(data[xInd][yInd][zInd] == value) > a=&data[xInd][yInd][zInd]; > } > > return a; > } This is noticably slower and less clear. I would not consider it an acceptable rewrite -- but I would consider the other form a good rewrite of it. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Keith Thompson on 28 Apr 2010 14:28 Seebs <usenet-nospam(a)seebs.net> writes: > On 2010-04-28, bart.c <bartc(a)freeuk.com> wrote: >> int* findvalue(int value) >> {int *a=0; >> >> for(int xInd = 0; !a && xInd < datasize; ++xInd) >> for(int yInd = 0; !a && yInd < dataxindsize; ++yInd) >> for(int zInd = 0; !a && zInd < dataxindyindsize; ++zInd) >> { >> if(data[xInd][yInd][zInd] == value) >> a=&data[xInd][yInd][zInd]; >> } >> >> return a; >> } > > This is noticably slower and less clear. I would not consider it > an acceptable rewrite -- but I would consider the other form a good > rewrite of it. I mostly agree, but -- "noticably slower"? How did you notice? I also wonder why Bartc changed the index type from size_t to int. For the body of the inner loop, I might consider writing: const int *p = &data[xInd][yInd][zInd]; if (*p == value) { return p; } For a sufficiently naive compiler, it might avoid recomputing the address; that by itself probably isn't a good enough reason to do it, since I suspect most decent optimizing compilers will avoid recomputing it anyway. But it also avoids writing a fairly complex expression twice, which could be helpful to the reader. A typo in one of the two occurrences of data[xInd][yInd][zInd] could result in a bug that's very difficult to track down. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Seebs on 28 Apr 2010 14:45 On 2010-04-28, Keith Thompson <kst-u(a)mib.org> wrote: > I mostly agree, but -- "noticably slower"? How did you notice? Good point. But I'd certainly *expect* it to be slower to add another test and a sequence point. I guess we could measure it. > For a sufficiently naive compiler, it might avoid recomputing > the address; that by itself probably isn't a good enough reason > to do it, since I suspect most decent optimizing compilers will > avoid recomputing it anyway. But it also avoids writing a fairly > complex expression twice, which could be helpful to the reader. > A typo in one of the two occurrences of data[xInd][yInd][zInd] > could result in a bug that's very difficult to track down. Yes. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Jeff Flinn on 28 Apr 2010 14:59
Keith Thompson wrote: > Seebs <usenet-nospam(a)seebs.net> writes: >> On 2010-04-28, bart.c <bartc(a)freeuk.com> wrote: >>> int* findvalue(int value) >>> {int *a=0; >>> >>> for(int xInd = 0; !a && xInd < datasize; ++xInd) >>> for(int yInd = 0; !a && yInd < dataxindsize; ++yInd) >>> for(int zInd = 0; !a && zInd < dataxindyindsize; ++zInd) >>> { >>> if(data[xInd][yInd][zInd] == value) >>> a=&data[xInd][yInd][zInd]; >>> } >>> >>> return a; >>> } >> This is noticably slower and less clear. I would not consider it >> an acceptable rewrite -- but I would consider the other form a good >> rewrite of it. > > I mostly agree, but -- "noticably slower"? How did you notice? Certainly is for the case data[0][0][0] == value. ;-) Jeff |