#include #include int test(int i) { switch (i) { case 1: return 0; case 4: return 1; case 9: return 2; case 16: return 3; case 25: return 4; case 36: return 5; case 49: return 6; case 88: return 7; default: return -1; } } int const n = 8; int in[] = { 1,4,9,16,25,36,49,88 }; //1,4,6,0,3,7,2 int g; typedef unsigned long ub4; typedef unsigned char ub1; ub1 tab[] = { 0, 2, 2, 7, 5, 0, 5, 0,}; /* The hash function */ ub4 phash(ub4 val) { ub4 a, b, rsl; b = (val >> 3) & 0x7; a = ((val << 31) >> 29); rsl = (a^tab[b]); return rsl; } int look[] = {0,0,0,0,0,0,0}; int main() { { boost::timer t; for (int i = 0; i < 100000000; ++i) { g = test(in[i & 7]); // if (g != i & 7) // { // std::cout << "switch error at:" << i << ", expected: " << (i & 7) << " got: " << g << std::endl; // break; // } } double el = t.elapsed(); std::cout << "switch took " << el << " seconds" << std::endl; } { for (int i = 0; i < 8; ++i) { std::cout << i << ", " << in[i] << ", " << phash(in[i]) << std::endl; look[phash(in[i])] = i; } std::cout << std::endl; std::cout << std::endl; for (int i = 0; i < 8; ++i) { std::cout << look[i] << std::endl; } std::cout << std::endl; std::cout << std::endl; boost::timer t; for (int i = 0; i < 100000000; ++i) { g = look[phash(in[i & 7])]; // std::cout << i << ", " << in[i & 7] << ", " << g << std::endl; // if (g != i % n) // { // std::cout << "phash error at:" << i << ", expected: " << (i & 7) << " got: " << g << std::endl; // break; // } } double el = t.elapsed(); std::cout << "phash took " << el << " seconds" << std::endl; } }