forked from lemire/rollinghashcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtesting.cpp
104 lines (93 loc) · 3.37 KB
/
speedtesting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <fstream>
#include "cyclichash.h"
#include "rabinkarphash.h"
#include "generalhash.h"
#include "threewisehash.h"
#include "ztimer.h"
using namespace std;
template<class hashfunction>
double hashALot( int n, int L, uint ttimes,uint sizeoftest , vector<uint32> & recorder) {
ZTimer t;
for(uint times = 0; times<ttimes; ++times) {
hashfunction hf(n,L);
for(uint k = 0; k<static_cast<uint>(n); ++k) {
hf.eat(static_cast<unsigned char>(k));
}
for(uint k = n; k<sizeoftest; ++k) {
hf.update(static_cast<unsigned char>(k-n),static_cast<unsigned char>(k));
}
/* The goal of the recorder is to prevent
the compiler from deciding that this whole computation
is not required!
*/
recorder.push_back(hf.hashvalue);
}
return t.split()/(1000.0*ttimes);
}
template<class hashfunction>
double hashALot( int n, int L, uint ttimes , vector<uint32> & recorder, vector<unsigned char> & data) {
ZTimer t;
for(uint times = 0; times<ttimes; ++times) {
hashfunction hf(n,L);
for(uint k = 0; k<static_cast<uint>(n); ++k) {
hf.eat(data[k]);
}
for(uint k = n; k<data.size(); ++k) {
hf.update(data[k-n],data[k]);
}
/* The goal of the recorder is to prevent
the compiler from deciding that this whole computation
is not required!
*/
recorder.push_back(hf.hashvalue);
}
return t.split()/1000.0;
}
void synthetic() {
int L = 19;
vector<uint32> recorder;
uint sizeoftest = 100000000;
cout<<"#n three-wise General BufferedGeneral Cyclic Karp-Rabin "<<endl;
for(uint n = 1; n+L<=32; ++n) {
cout<<n<<" "<<hashALot<ThreeWiseHash<> >(n,L,1,sizeoftest,recorder)<<" ";
cout<<hashALot<GeneralHash<NOPRECOMP> >(n,L,1,sizeoftest,recorder)<<" ";
cout<<hashALot<GeneralHash<FULLPRECOMP> >(n,L,1,sizeoftest,recorder)<<" ";
cout<<hashALot<CyclicHash<> >(n,L+n,1,sizeoftest,recorder)<< " ";
cout<<hashALot<KarpRabinHash<> >(n,L,1,sizeoftest,recorder)<<endl;
}
cout <<"# L= "<<L<<" char-length= "<<sizeoftest<<endl;
}
void grabFileContent(vector<unsigned char> & data, string filename) {
string line;
ifstream file(filename.c_str());
getline(file, line);
while ( file.good() ) {
getline(file, line);
for(uint k = 0; k<line.size(); ++k)
data.push_back(line[k]);//presumably not very fast to do it char by char
}
file.close();
}
void realdata(string filename) {
int L = 19;
vector<uint32> recorder;
uint repeats=1;
vector<unsigned char> data;
grabFileContent(data, filename);
cout<<"#n three-wise General BufferedGeneral Cyclic Karp-Rabin "<<endl;
for(uint n = 1; n+L<=32; ++n) {
cout<<n<<" "<<hashALot<ThreeWiseHash<> >(n,L,repeats,recorder,data)<<" ";
cout<<hashALot<GeneralHash<NOPRECOMP> >(n,L,repeats,recorder,data)<<" ";
cout<<hashALot<GeneralHash<FULLPRECOMP> >(n,L,repeats,recorder,data)<<" ";
cout<<hashALot<CyclicHash<> >(n,L+n,repeats,recorder,data)<< " ";
cout<<hashALot<KarpRabinHash<> >(n,L,repeats,recorder,data)<<endl;
}
cout <<"# L= "<<L<<" char-length= "<<data.size()<< " repeats="<<repeats<<endl;
}
int main(int params, char ** args) {
if (params == 1)
synthetic();
else
realdata(args[1]);
return 0;
}