Ninja
Main Page
Namespaces
Classes
Files
File List
File Members
hash_collision_bench.cc
Go to the documentation of this file.
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "
build_log.h
"
16
17
#include <algorithm>
18
using namespace
std;
19
20
#include <time.h>
21
22
int
random
(
int
low,
int
high) {
23
return
int(low + (rand() /
double
(RAND_MAX)) * (high - low) + 0.5);
24
}
25
26
void
RandomCommand
(
char
** s) {
27
int
len =
random
(5, 100);
28
*s =
new
char
[len];
29
for
(
int
i = 0; i < len; ++i)
30
(*s)[i] = (char)
random
(32, 127);
31
}
32
33
int
main
() {
34
const
int
N = 20 * 1000 * 1000;
35
36
// Leak these, else 10% of the runtime is spent destroying strings.
37
char
** commands =
new
char
*[N];
38
pair<uint64_t, int>* hashes =
new
pair<uint64_t, int>[N];
39
40
srand((
int
)time(NULL));
41
42
for
(
int
i = 0; i < N; ++i) {
43
RandomCommand
(&commands[i]);
44
hashes[i] = make_pair(
BuildLog::LogEntry::HashCommand
(commands[i]), i);
45
}
46
47
sort(hashes, hashes + N);
48
49
int
num_collisions = 0;
50
for
(
int
i = 1; i < N; ++i) {
51
if
(hashes[i - 1].first == hashes[i].first) {
52
if
(strcmp(commands[hashes[i - 1].second],
53
commands[hashes[i].second]) != 0) {
54
printf(
"collision!\n string 1: '%s'\n string 2: '%s'\n"
,
55
commands[hashes[i - 1].second],
56
commands[hashes[i].second]);
57
num_collisions++;
58
}
59
}
60
}
61
printf(
"\n\n%d collisions after %d runs\n"
, num_collisions, N);
62
}
main
int main()
Definition:
hash_collision_bench.cc:33
RandomCommand
void RandomCommand(char **s)
Definition:
hash_collision_bench.cc:26
random
int random(int low, int high)
Definition:
hash_collision_bench.cc:22
build_log.h
BuildLog::LogEntry::HashCommand
static uint64_t HashCommand(StringPiece command)
Definition:
build_log.cc:90
Generated on Sat May 31 2014 08:04:22 for Ninja by
1.8.7