Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update to documentation, and added a new strongly universal hash func…

…tion.
  • Loading branch information...
commit ab3613e6f6e71a18abfcb403a3b1645e3dc3d9bc 1 parent 53b5337
@lemire authored
Showing with 69 additions and 11 deletions.
  1. +26 −4 hashfunctions.h
  2. +43 −7 speedtesting.cpp
View
30 hashfunctions.h
@@ -38,7 +38,7 @@ class Silly {
/*
-* This is just like StrongMultilinear but with a bit of loop unrolling. Almost universal.
+* This is just like StrongMultilinear but with a bit of loop unrolling. Strongly universal.
* Assumes an even number of elements.
* Also assumes that the string does not exceed the size of the provided randombuffer (no checking).
*/
@@ -62,7 +62,7 @@ class StrongMultilinear {
};
/*
-* This is just like StrongMultilinear but with a bit of loop unrolling. Almost universal.
+* This is just like StrongMultilinear but with a bit of loop unrolling. Strongly universal.
* Assumes an even number of elements.
* Also assumes that the string does not exceed the size of the provided randombuffer (no checking).
*/
@@ -85,9 +85,8 @@ class StrongMultilinearTwoByTwo {
const vector<uint64> & firstfew;
};
-
/*
-* From Thorup, SODA 09. Almost universal.
+* From Thorup, SODA 09. Almost universal. **Not strongly universal**
* Assumes an even number of elements.
* Also assumes that the string does not exceed the size of the provided randombuffer (no checking).
*/
@@ -110,6 +109,29 @@ class Thorup {
};
+/**
+* A bit like what Thorup proposed in SODA 09, but actually strongly universal.
+* Assumes an even number of elements.
+* Also assumes that the string does not exceed the size of the provided randombuffer (no checking).
+*/
+class XAMA {
+ public :
+ XAMA(const vector<uint64> & randombuffer) : firstfew(randombuffer) {
+ }
+
+ template <class INTEGER>
+ uint32 hash(const INTEGER * p, const INTEGER * const endp) const {
+ const uint64 * randomdata = & firstfew[0];
+ uint64 sum = *(randomdata++);
+ for(;p!=endp;p+=2,randomdata+=2) {
+ sum += (*randomdata + *p) * (*(randomdata+1) + *(p+1));
+ }
+ return static_cast<uint32>(sum>>32);
+ }
+
+ const vector<uint64> & firstfew;
+
+};
// just for testing purposes
class NoMultiplication {
public:
View
50 speedtesting.cpp
@@ -30,14 +30,26 @@ double testSpeed(const T & hasher, const vector<INTEGER> & data, uint64 & answer
return timeelapsed;
}
+template <class T, class INTEGER>
+double testSpeedManyTimes(const T & hasher, const vector<INTEGER> & data, uint64 & answer, const uint mN,const uint times) {
+ ZTimer t;
+ double timeelapsed;
+ t.reset();
+ for(uint k = 0; k<times;++k)
+ answer += hasher.hash(&data[0],&data[0]+mN);
+ timeelapsed = t.split()/(1000.0);
+ return timeelapsed;
+}
int main(int params, char ** args) {
uint N;
if (params >= 2)
N = atoi(args[1]);
else {
- N = 268435456;
+ N = 1024*1024;
}
+ cout<<"#sizeof(uint64)="<<sizeof(uint64)<<endl;
+ assert(sizeof(uint64)==8);
cout<<"# Initializing data..."<<endl;
vector<uint32>data(N);
vector<uint64>randombuffer64(N+1);
@@ -62,19 +74,43 @@ int main(int params, char ** args) {
Silly silly;
Thorup thorup(randombuffer64);
StrongMultilinear sm(randombuffer64);
+ XAMA xama(randombuffer64);
+ NoMultiplication testing(randombuffer64);
+ StrongMultilinearTwoByTwo sm2by2(randombuffer64);
+ const uint shorttimes =2000000;
+ cout<<"# Starting tests... repeating each run "<<shorttimes<<" times"<<endl;
+ cout<<"# N silly thorup09(not-strongly-universal) xama strong-multilinear strong-multilinear-2by2"<<endl;
+ for(uint mN = 1024; mN<=2048; mN*=2) {
+ vector<double> counter(5,0);
+ counter[0]+=testSpeedManyTimes(silly,data,answer,mN,shorttimes);
+ counter[1]+=testSpeedManyTimes(thorup,data,answer,mN,shorttimes);
+ counter[2]+=testSpeedManyTimes(xama,data,answer,mN,shorttimes);
+ counter[3]+=testSpeedManyTimes(sm,data,answer,mN,shorttimes);
+ counter[4]+=testSpeedManyTimes(sm2by2,data,answer,mN,shorttimes);
+ cout<<mN<<" ";
+ for(uint k = 0; k<counter.size();++k)
+ cout<<counter[k]<<" ";
+ cout<<endl;
+ }
+ cout<<endl;
cout<<"# Starting tests... repeating each run "<<times<<" times"<<endl;
- cout<<"# N silly thorup09 strong-multilinear strong-multilinear-2by2 commoniterative "<<endl;
+ cout<<"# N silly thorup09(not-strongly-universal) xama strong-multilinear strong-multilinear-2by2 "<<endl;
for(uint mN = 1048576; mN<=data.size(); mN*=2) {
- vector<double> counter(4,0);
- StrongMultilinearTwoByTwo sm2by2(randombuffer64);
+ vector<double> counter(5,0);
for(uint k = 0;k<times;++k) {
counter[0]+=testSpeed(silly,data,answer,mN);
counter[1]+=testSpeed(thorup,data,answer,mN);
- counter[2]+=testSpeed(sm,data,answer,mN);
- counter[3]+=testSpeed(sm2by2,data,answer,mN);
+ counter[2]+=testSpeedManyTimes(xama,data,answer,mN,shorttimes);
+ counter[3]+=testSpeed(sm,data,answer,mN);
+ counter[4]+=testSpeed(sm2by2,data,answer,mN);
}
- cout<<mN<<" "<<counter[0]<<" "<<counter[1]<<" "<< counter[2]<< " "<<counter[3]<<" "<<counter[4]<<endl;
+ cout<<mN<<" ";
+ for(uint k = 0; k<counter.size();++k)
+ cout<<counter[k]<<" ";
+ cout<<endl;
}
+ cout<<endl;
+
return answer;
}
Please sign in to comment.
Something went wrong with that request. Please try again.