Repository navigation
Expand file tree
/
Copy pathassignment_benchmark.cc
More file actions
131 lines (112 loc) · 4.21 KB
/
Copy pathassignment_benchmark.cc
File metadata and controls
131 lines (112 loc) · 4.21 KB
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
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdint>
#include <vector>
#include "benchmark/benchmark.h"
#include "ortools/constraint_solver/assignment.h"
#include "ortools/constraint_solver/constraint_solver.h"
namespace operations_research {
namespace {
static const int64_t kMediumAssignments = 1 << 11;
static const int64_t kLargeAssignments = 1 << 12;
static const int64_t kMin = 0;
static const int64_t kMax = 10;
#define CP_BM_AssignmentAction(action) \
std::vector<IntVar*> vars; \
solver.MakeIntVarArray(assignment_size, kMin, kMax, "", &vars); \
for (auto _ : state) { \
state.PauseTiming(); \
Assignment assignment(&solver); \
state.ResumeTiming(); \
for (int j = 0; j < assignment_size; ++j) { \
assignment.action(vars[j]); \
} \
}
void BM_AssignmentAdd(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentAdd");
CP_BM_AssignmentAction(Add);
}
BENCHMARK(BM_AssignmentAdd)->Range(1, kLargeAssignments);
void BM_AssignmentFastAdd(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentFastAdd");
CP_BM_AssignmentAction(FastAdd);
}
BENCHMARK(BM_AssignmentFastAdd)->Range(1, kLargeAssignments);
void BM_AssignmentElement(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentFastAdd");
Assignment assignment(&solver);
std::vector<IntVar*> vars;
solver.MakeIntVarArray(assignment_size, kMin, kMax, "", &vars);
assignment.Add(vars);
const Assignment::IntContainer& container = assignment.IntVarContainer();
for (auto s : state) {
for (int j = 0; j < assignment_size; ++j) {
container.Element(vars[j]);
}
}
}
BENCHMARK(BM_AssignmentElement)->Range(1, kLargeAssignments);
BENCHMARK(BM_AssignmentElement)
->Arg(1)
->Arg(3)
->Arg(7)
->Arg(9)
->Arg(10)
->Arg(11)
->Arg(12)
->Arg(13);
void BM_AssignmentCopyIntersection(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentCopyIntersection");
Assignment assignment1(&solver);
Assignment assignment2(&solver);
std::vector<IntVar*> vars;
solver.MakeIntVarArray(assignment_size, kMin, kMax, "", &vars);
assignment1.Add(vars);
assignment2.Add(vars);
for (auto s : state) {
assignment1.CopyIntersection(&assignment2);
}
}
BENCHMARK(BM_AssignmentCopyIntersection)->Range(1, kMediumAssignments);
void BM_AssignmentClone(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentClone");
Assignment assignment1(&solver);
std::vector<IntVar*> vars;
solver.MakeIntVarArray(assignment_size, kMin, kMax, "", &vars);
assignment1.Add(vars);
for (auto s : state) {
Assignment assignment2(&assignment1);
}
}
BENCHMARK(BM_AssignmentClone)->Range(1, kMediumAssignments);
void BM_AssignmentCopy(benchmark::State& state) {
const int assignment_size = state.range(0);
Solver solver("BM_AssignmentCopy");
Assignment assignment1(&solver);
Assignment assignment2(&solver);
std::vector<IntVar*> vars;
solver.MakeIntVarArray(assignment_size, kMin, kMax, "", &vars);
assignment1.Add(vars);
for (auto s : state) {
assignment2.Copy(&assignment1);
}
}
BENCHMARK(BM_AssignmentCopy)->Range(1, kMediumAssignments);
#undef CP_BM_AssignmentAction
} // namespace
} // namespace operations_research