-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy patharray_scans.cc
152 lines (127 loc) · 3.37 KB
/
array_scans.cc
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Benchmark to compare naive C array scan, stream scan and binary search on
* different array sizes. There is no memory transfers, so the becnhmark is
* pure CPU loading.
*
* Results for Bsearch optimal at Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz:
*
* binary search: : 27ms
* scan: : 617ms
* scan stream: : 65ms
*
* For Scan optimal:
*
* binary search: : 245ms
* scan: : 556ms
* scan stream: : 136ms
*
* So binary search is always faster than naive C array scan, ever for scan
* in only one cache line. However, it's slower than stream scan for smal data.
*
* Copyright (C) 2015 Alexander Krizhanovsky ([email protected]).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <iostream>
#include <sstream>
#if 0
// Bsearch optimal
static const size_t N = 1024;
static const size_t ITER = 256 * 1024;
#else
// Scan optimal (twice worse actually!)
static const size_t N = 16;
static const size_t ITER = 16 * 1024 * 1024;
#endif
#define BENCHMARK_START() \
struct timeval tv0, tv1; \
gettimeofday(&tv0, NULL);
#define BENCHMARK_RESULT(desc) \
gettimeofday(&tv1, NULL); \
tv0.tv_usec = (tv0.tv_sec * 1000 * 1000 + tv0.tv_usec) / 1000; \
tv1.tv_usec = (tv1.tv_sec * 1000 * 1000 + tv1.tv_usec) / 1000; \
std::cout << (desc) << ": " << (tv1.tv_usec - tv0.tv_usec) \
<< "ms" << std::endl;
inline int
bsearch(unsigned int *tbl, unsigned int key)
{
int i = N / 2, d = (N + 3) / 4;
while (1) {
if (tbl[i] == key)
return i;
if (!d) {
if (key < tbl[i] && i && tbl[--i] == key)
return i;
if (key > tbl[i] && ++i < N && tbl[i] == key)
return i;
return -1;
}
i += (key < tbl[i]) ? -d : d;
d /= 2;
}
}
inline int
scan(unsigned int *tbl, unsigned int key)
{
for (int i = 0; i < N; ++i)
if (tbl[i] == key)
return i;
return -1;
}
inline int
scan_stream(unsigned int *tbl, unsigned int key)
{
return !!memchr(tbl, key, N * sizeof(int));
}
int
main()
{
int key = 1;
unsigned int *area;
if (posix_memalign((void **)&area, 4096, N * sizeof(int) * ITER)) {
std::cerr << "fail memaign" << std::endl;
return 1;
}
memset(area, 0, N * sizeof(int) * ITER);
{
std::stringstream bres;
bres << "binary search: ";
BENCHMARK_START();
for (int i = 0; i < ITER; ++i)
bsearch(area + i * N, key);
BENCHMARK_RESULT(bres.str());
}
{
std::stringstream bres;
bres << "scan: ";
BENCHMARK_START();
for (int i = 0; i < ITER; ++i)
scan(area + i * N, key);
BENCHMARK_RESULT(bres.str());
}
{
std::stringstream bres;
bres << "scan stream: ";
BENCHMARK_START();
for (int i = 0; i < ITER; ++i)
scan_stream(area + i * N, key);
BENCHMARK_RESULT(bres.str());
}
free(area);
return 0;
}