Branch data Line data Source code
1 : : /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
2 : : // $Id: TwoWise.cc 1386 2005-09-14 21:42:13Z vern $
3 : : //
4 : : // Implementation of 2-wise independent hash functions. Contributed
5 : : // by Yin Zhang.
6 : : //
7 : :
8 : : #include <stdlib.h>
9 : :
10 : : #include "TwoWise.h"
11 : :
12 : 0 : TwoWise::TwoWise(int arg_dim)
13 : : {
14 : 0 : dim = arg_dim;
15 : 0 : int n = dim > 2 ? dim : 2;
16 : :
17 : 0 : a = new uint64[n];
18 : 0 : b = new uint64[n];
19 : 0 : c = new uint32[n];
20 : :
21 [ # # ][ # # ]: 0 : for ( int i = 0; i < n; ++i )
22 : : {
23 : 0 : a[i] = rand64bit() & ~(1ULL);
24 : 0 : b[i] = rand64bit() & ~(1ULL);
25 : 0 : c[i] = 0;
26 : : }
27 : :
28 : 0 : a0 = a[0];
29 : 0 : b0 = b[0];
30 : 0 : a1 = a[1];
31 : 0 : b1 = b[1];
32 : 0 : }
33 : :
34 : 0 : TwoWise::~TwoWise()
35 : : {
36 [ # # ][ # # ]: 0 : delete[] a;
37 [ # # ][ # # ]: 0 : delete[] b;
38 [ # # ][ # # ]: 0 : delete[] c;
39 : 0 : }
40 : :
41 : 0 : void TwoWise::TestSpeed(uint32 N)
42 : : {
43 : 0 : uint32 x = 0, i;
44 : :
45 : 0 : double start_time = current_time();
46 [ # # ]: 0 : for ( i = 0; i < N; ++i )
47 : 0 : x ^= Hash(i);
48 : 0 : double end_time = current_time();
49 : 0 : double time0 = end_time - start_time;
50 : :
51 : 0 : start_time = current_time();
52 [ # # ]: 0 : for ( i = 0; i < N; ++i )
53 : 0 : x ^= Hash(i, i);
54 : 0 : end_time = current_time();
55 : 0 : double time1 = end_time - start_time;
56 : :
57 : : fprintf(stderr, "time0=%.6f time1=%.6f x=%u\n",
58 : 0 : time0, time1, x);
59 : 0 : }
|