Ninja
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
19
#include <stdlib.h>
20
#include <time.h>
21
22
using namespace
std
;
23
24
int
random
(
int
low,
int
high) {
25
return
int(low + (rand() /
double
(RAND_MAX)) * (high - low) + 0.5);
26
}
27
28
void
RandomCommand
(
char
** s) {
29
int
len =
random
(5, 100);
30
*s =
new
char
[len+1];
31
for
(
int
i = 0; i < len; ++i)
32
(*s)[i] = (char)
random
(32, 127);
33
(*s)[len] =
'\0'
;
34
}
35
36
int
main
() {
37
const
int
N = 20 * 1000 * 1000;
38
39
// Leak these, else 10% of the runtime is spent destroying strings.
40
char
** commands =
new
char
*[N];
41
pair<uint64_t, int>* hashes =
new
pair<uint64_t, int>[N];
42
43
srand((
int
)time(NULL));
44
45
for
(
int
i = 0; i < N; ++i) {
46
RandomCommand
(&commands[i]);
47
hashes[i] = make_pair(
BuildLog::LogEntry::HashCommand
(commands[i]), i);
48
}
49
50
sort(hashes, hashes + N);
51
52
int
collision_count = 0;
53
for
(
int
i = 1; i < N; ++i) {
54
if
(hashes[i - 1].first == hashes[i].first) {
55
if
(strcmp(commands[hashes[i - 1].second],
56
commands[hashes[i].second]) != 0) {
57
printf(
"collision!\n string 1: '%s'\n string 2: '%s'\n"
,
58
commands[hashes[i - 1].second],
59
commands[hashes[i].second]);
60
collision_count++;
61
}
62
}
63
}
64
printf(
"\n\n%d collisions after %d runs\n"
, collision_count, N);
65
}
build_log.h
RandomCommand
void RandomCommand(char **s)
Definition:
hash_collision_bench.cc:28
random
int random(int low, int high)
Definition:
hash_collision_bench.cc:24
main
int main()
Definition:
hash_collision_bench.cc:36
std
Definition:
hash_map.h:26
BuildLog::LogEntry::HashCommand
static uint64_t HashCommand(StringPiece command)
Definition:
build_log.cc:60
Generated by
1.9.1