Usually we use lookup tables (result of multi-calculations) so that we can access them faster by not doing the calculation again, right? You may be surprised to learn that this approach may actually make your functions slower....
Code: Select all
#include <stdlib.h>
#include <iostream>
unsigned int looktable[65536];
using namespace std;
void lookup(int count)
{
unsigned int rndloc;
for(int i = 0;i < count;i++)
{
rndloc = rand() & 0xFFFF;
rndloc = looktable[rndloc] + 2;
}
}
void calculate(int count)
{
unsigned int rndloc;
for(int i = 0;i < count;i++)
{
rndloc = rand() & 0x7FFF;
rndloc = (rndloc * rndloc) + (rndloc * 5) + 2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int key;
cout << "Press a key to run LOOKUP Test...";
cin >> key;
lookup(1000000000);
cout << "Press a key to run CALCULATE Test...";
cin >> key;
calculate(1000000000);
cout << "Done...";
cin >> key;
return 0;
}
- one is using a random location in a lookup table and adds 2 to it, so its just a lookup + add
- the other calculates the value instead of looking it up and this calculation is 2 multiplications + an addition, quite expensive compared to lookup, right? WRONG!!
I didnt get deeper into the subject, but soon i will, still the result show that the BRUTE FORCE CALCULATION is %10 faster on my machine and it will continue to get faster directly proportional to the CPU Power, where lookup performance will remain same.
Also keep in mind that both functions use a very costly rand() function. When the cost of rand() is stripped, brute force calculation's performance will probably be magnitudes better than lookups, or you can get same performance but do quite more complicated calculations...
So, why?
Very easy answer, the CPUs got so much powerfull, now they can run multiple instructions per clock, instruction clock counts decreased etc. But, accessing memory is still as slow as lets say 5 years ago, even worse, engineers made it so that memory access is faster sequentially compared to previous yers, but also, a cache miss (caused by random access) is much more expensive than before.
So, using lookup tables, you access the memory randomly (a random index usually, thats why we use lookup tables) and the cost of cache misses is equal to hundreds of instructions. So if your calculation can be done in lets say 100 instructions, using the brute force calculation method may yield better results than your lookup table...
Just my 2 cents...