Author: Dave Gomboc
Date: 04:04:29 01/01/03
Go up one level in this thread
>>>This was only thinking about a theoretical case but I found other things in my >>>code. >>> >>>In a lot of places in my program >>>I have a condition like >>> >>>while (target!=-1&&info[target]==EMPTY) >>> >>>In most cases target!=-1 >>> >>>I thought that I need to do the && in this order >>>Do you say that I can change the order without risks? >>> >>>Suppose that the loop stops in most of the cases because of the fact that >>>info[target]=EMPTY. >>> >>>Does it going to do my program faster or maybe the compiler can detect these >>>cases in profile optimazions? >>> >>>I thought that even checking the value of info[-1] can give an error or change >>>some varaible that I do not want. >>> >>>Uri >> >>Technically, checking the value of info[-1] could even cause your program to >>crash. But in practice, as long as your program has the ability to access the >>memory immediately in front of info, it will probably just read the value in >>that memory location. >> >>To be slightly safer, you can declare a padded_info like I suggested elsewhere >>in this thread. That way even assignments to info[-1] won't cause a problem -- >>on most architectures, with most compilers. >> >>>while (target!=-1&&info[target]==EMPTY) >> >>Technically it would be wrong to flip this test around, but in practice you can >>probably get away with it. >> >>Dave > >originally info is a global array and >I have int info[64]; > >If I understand correctly >Based on your example I can use in data.c > >int PADDED_info[65]; >int * const info = PADDED_info+1; Yes. >I guess that in this case info[-1]=PADDED_info[0] Exactly. >I guess also that in this case I can change the condition to >while (info[target]==EMPTY) >when info[-1] has a constant value that is different from 0. Yes -- but how can you be sure the value will stay constant? You might assign to it by accident. But still, yes, if you are sure you don't change it, then you can skip the test for (target != -1). Of course, if target == -5, or target == 12096, you've still got a problem. >I also guess that the speed of using info instead of using the original array >is the same. Yes. >Am I correct? > >I have doubts about the last point because 64 is a nice number and I suspect >that my computer will like less 65. If you want, you could test using 72 instead of 65 (and compare to 64) to see if it actually makes any difference. If there even is a speed difference, I doubt it will be noticeable. >Uri Dave
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.