Sven Sch?le wrote:Hi, Zach,
just a small note on your note ... I agree to everything you wrote in principle. But if TIMENODES is a compile-time constant and a power of 2 then
- Code: Select all
if (!(nodes % TIMENODES)) { ...
should be fast enough, probably even faster than:
- Code: Select all
if (!(nodes & (TIMENODES-1)) {...
But even if the value of TIMENODES is not known for the compiler (so that he can't transform the modulo into a shift) then this looks preferrable to me because it is the most simple and straightforward way of writing "every X nodes".
Sven
Sven, I am not saying, that it will make a lot of difference, but modulo is typically the slowest operation on computers (together with devide - often, for example on x86, both devide and modulo are done together by one opcode. On some computers, modulo is done by some subroutine calls). Be very careful, when the node counter is 64 bit - the the modulo will probably be very ineffecient on every 32-bit computer.
I use:
- Code: Select all
/* at some suitable place before calling absearch() initialize checknodes */
absearch(...)
{
...
++node_counter;
if (--checknodes <= 0)
{
/* Do time over and input available checks here */
/* And reset checknodes to some suitable value.
The actual value may be calculated dynamically */
}
...
This should not be slower than the and method, should be less prone to coding errors (not the first time, I saw code in fora, that got the and method wrong), and even is a bit more general, because you don't need to have a constant number of nodes, when you want to check. (Or at least you save the hazzle, to calculate the closest power of two, to the actual number).
The overhead of the above method is one branch, and one decrement in every call to absearch (no real need to do it in qsearch). Certainly faster than modulo, probably not slower than the and method.
Cheers,
Dieter