Chances are, your table will be slower, especially if it is decent size.
A table look up means an address calculation and a memory fetch, which
even with superscalar architectures and large caches is likely to cost
you more than a single instruction register-register AND or shift.
By the way, why would you need to build the table in a different thread?
To waste memory and cycles?
You'd be better off with listening to what the others replied to your
question. Unless x must be signed, change it to unsigned and the
compiler will merrily optimise your division away.
Unfortunately x must be signed because -1 is used to denote "invalid
value". So I cannot take full advantage of people's suggestions.
I have tried these out. I find that the program speed does not seem to
change much, if I use x%8 or x&7 or x=-(x>>3)<<3 : disappointing! I did
not try the table yet, but it stands to reason that it will be quicker
just to look up a result, than to do a computation every time.
I have two other optimisation questions I am looking at.
In one loop called many times, I need to swap 2 variables. Currently I do
this with a temporary variable:
tmp=a; a=b; b=tmp;
I think it will save overhead to avoid this extra variable. I found on
the internet an "XOR trick", b^=a; a^=b; b^a=a; but the article said it
can be dangerous in some cases i.e. overflow.
I was thinking of this and I invented an alternative version which I
believe will work correctly in 100% of cases (uses SUM not XOR):
b+=a; a=b-a; b-=a;
Has anyone thought of this idea before? Would you say it will be cheaper
than the temporary variable?
Also program start up is very slow. It must read in 10 million data
points from a text file - access is slow (network file). There are 2
unsigned long integers (x,y) on each line (represented by strings) that I
read into a 10000000x2 array.
Right now I am using fgets() and atol. I don't have any good ideas to
optimise this except maybe setting up 100 threads to read different
sections of the file. Are there any faster ways to convert integers than
atol?
while(fgets(strg,999,fp))
{ ary[ln][0]=atol(strtok(strg," "));
ary[ln][1]=atol(strtok((char*)NULL,"\n"));
ln++; }
Thank You to everyone who has helped so far.