# benchmark
[![Build Status](https://travis-ci.org/google/benchmark.svg?branch=master)](https://travis-ci.org/google/benchmark)
[![Build status](https://ci.appveyor.com/api/projects/status/u0qsyp7t1tk7cpxs/branch/master?svg=true)](https://ci.appveyor.com/project/google/benchmark/branch/master)
[![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark)
A library to support the benchmarking of functions, similar to unit-tests.
Discussion group: https://groups.google.com/d/forum/benchmark-discuss
IRC channel: https://freenode.net #googlebenchmark
[Known issues and common problems](#known-issues)
[Additional Tooling Documentation](docs/tools.md)
## Example usage
### Basic usage
Define a function that executes the code to be measured.
```c++
static void BM_StringCreation(benchmark::State& state) {
while (state.KeepRunning())
std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);
// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
while (state.KeepRunning())
std::string copy(x);
}
BENCHMARK(BM_StringCopy);
BENCHMARK_MAIN();
```
### Passing arguments
Sometimes a family of benchmarks can be implemented with just one routine that
takes an extra argument to specify which one of the family of benchmarks to
run. For example, the following code defines a family of benchmarks for
measuring the speed of `memcpy()` calls of different lengths:
```c++
static void BM_memcpy(benchmark::State& state) {
char* src = new char[state.range(0)];
char* dst = new char[state.range(0)];
memset(src, 'x', state.range(0));
while (state.KeepRunning())
memcpy(dst, src, state.range(0));
state.SetBytesProcessed(int64_t(state.iterations()) *
int64_t(state.range(0)));
delete[] src;
delete[] dst;
}
BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
```
The preceding code is quite repetitive, and can be replaced with the following
short-hand. The following invocation will pick a few appropriate arguments in
the specified range and will generate a benchmark for each such argument.
```c++
BENCHMARK(BM_memcpy)->Range(8, 8<<10);
```
By default the arguments in the range are generated in multiples of eight and
the command above selects [ 8, 64, 512, 4k, 8k ]. In the following code the
range multiplier is changed to multiples of two.
```c++
BENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10);
```
Now arguments generated are [ 8, 16, 32, 64, 128, 256, 512, 1024, 2k, 4k, 8k ].
You might have a benchmark that depends on two or more inputs. For example, the
following code defines a family of benchmarks for measuring the speed of set
insertion.
```c++
static void BM_SetInsert(benchmark::State& state) {
while (state.KeepRunning()) {
state.PauseTiming();
std::set<int> data = ConstructRandomSet(state.range(0));
state.ResumeTiming();
for (int j = 0; j < state.range(1); ++j)
data.insert(RandomNumber());
}
}
BENCHMARK(BM_SetInsert)
->Args({1<<10, 1})
->Args({1<<10, 8})
->Args({1<<10, 64})
->Args({1<<10, 512})
->Args({8<<10, 1})
->Args({8<<10, 8})
->Args({8<<10, 64})
->Args({8<<10, 512});
```
The preceding code is quite repetitive, and can be replaced with the following
short-hand. The following macro will pick a few appropriate arguments in the
product of the two specified ranges and will generate a benchmark for each such
pair.
```c++
BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {1, 512}});
```
For more complex patterns of inputs, passing a custom function to `Apply` allows
programmatic specification of an arbitrary set of arguments on which to run the
benchmark. The following example enumerates a dense range on one parameter,
and a sparse range on the second.
```c++
static void CustomArguments(benchmark::internal::Benchmark* b) {
for (int i = 0; i <= 10; ++i)
for (int j = 32; j <= 1024*1024; j *= 8)
b->Args({i, j});
}
BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
```
### Calculate asymptotic complexity (Big O)
Asymptotic complexity might be calculated for a family of benchmarks. The
following code will calculate the coefficient for the high-order term in the
running time and the normalized root-mean square error of string comparison.
```c++
static void BM_StringCompare(benchmark::State& state) {
std::string s1(state.range(0), '-');
std::string s2(state.range(0), '-');
while (state.KeepRunning()) {
benchmark::DoNotOptimize(s1.compare(s2));
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_StringCompare)
->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN);
```
As shown in the following invocation, asymptotic complexity might also be
calculated automatically.
```c++
BENCHMARK(BM_StringCompare)
->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity();
```
The following code will specify asymptotic complexity with a lambda function,
that might be used to customize high-order term calculation.
```c++
BENCHMARK(BM_StringCompare)->RangeMultiplier(2)
->Range(1<<10, 1<<18)->Complexity([](int n)->double{return n; });
```
### Templated benchmarks
Templated benchmarks work the same way: This example produces and consumes
messages of size `sizeof(v)` `range_x` times. It also outputs throughput in the
absence of multiprogramming.
```c++
template <class Q> int BM_Sequential(benchmark::State& state) {
Q q;
typename Q::value_type v;
while (state.KeepRunning()) {
for (int i = state.range(0); i--; )
q.push(v);
for (int e = state.range(0); e--; )
q.Wait(&v);
}
// actually messages, not bytes:
state.SetBytesProcessed(
static_cast<int64_t>(state.iterations())*state.range(0));
}
BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
```
Three macros are provided for adding benchmark templates.
```c++
#if __cplusplus >= 201103L // C++11 and greater.
#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters.
#else // C++ < C++11
#define BENCHMARK_TEMPLATE(func, arg1)
#endif
#define BENCHMARK_TEMPLATE1(func, arg1)
#define BENCHMARK_TEMPLATE2(func, arg1, arg2)
```
## Passing arbitrary arguments to a benchmark
In C++11 it is possible to define a benchmark that takes an arbitrary number
of extra arguments. The `BENCHMARK_CAPTURE(func, test_case_name, ...args)`
macro creates a benchmark that invokes `func` with the `benchmark::State` as
the first argument followed by the specified `args...`.
The `test_case_name` is appended to the name of the benchmark and
should describe the values passed.
```c++
template <class ...ExtraArgs>`
void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
[...]
}
// Registers a benchmark named "BM_takes_args/int_string_test` that passes
// the specified values to `extra_args`.
BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
```
Note that elements of `...args` may refer to global variables. Users should
avoid modifying global state inside of a benchmark.
## Using RegisterBenchmark(name, fn, args...)
The `RegisterBenchmark(name, func, args...)` function provides an alternative
way to create and register benchmarks.
`RegisterBenchmark(name, func, args...)` creates, registers, and returns a
pointer to a new benchmark with the specified `name` that invokes
`func(st, args...)` where `st` is a `benchmark::State` object.
Unlike the `BENCHMARK` registration macros, which can only be used at the global
scope, the `RegisterBenchmark` can be called anywhere. This allows for
benchmark tests to be registered programmatically.
Additionally `RegisterBenchmark` allows any callable object to be registered
as a benchmark. Including capturing lambdas and function objects. This
allows the creation
For Example:
```c++
auto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ };
int main(int argc, char** argv) {
没有合适的资源?快使用搜索试试~ 我知道了~
cartographer-melodic-devel.zip
共2000个文件
cc:1212个
h:1019个
proto:351个
需积分: 50 25 下载量 153 浏览量
2020-09-03
19:03:33
上传
评论 1
收藏 19.51MB ZIP 举报
温馨提示
快速完成基于Ubuntu18.04的Cartographer以及Cartographer_ros安装脚本。 Cartographer由谷歌一直在维护,从之前的tf1到现在melodic版本上的tf2_ros都有更新支持。而且cartographer所依赖的库也在一直更新,包括abseil-cpp,ceres-solver,protobuf等,其依赖的版本混乱,并不能相互兼容。经过测试,在2019年的某个版本解决好依赖问题之后,同样的方法在2020年clone新的官方代码就编译出错了。并且官方只更新代码,并没有对安装文档进行对应的更新,
资源推荐
资源详情
资源评论
收起资源包目录
cartographer-melodic-devel.zip (2000个子文件)
upb.c 409KB
upb.c 409KB
php-upb.c 265KB
message.c 106KB
defs.c 77KB
encode_decode.c 77KB
encode_decode.c 59KB
storage.c 41KB
storage.c 40KB
def.c 38KB
def.c 32KB
message.c 28KB
type_check.c 26KB
map.c 25KB
message.c 24KB
repeated_field.c 22KB
map.c 20KB
array.c 19KB
array.c 19KB
map.c 18KB
protobuf.c 18KB
convert.c 14KB
protobuf.c 12KB
names.c 8KB
curve_fitting.c 6KB
protobuf.c 6KB
arena.c 3KB
utf8.c 3KB
wrap_memcpy.c 2KB
jsoncpp.cpp 147KB
posix_regex.cpp 297B
gnu_posix_regex.cpp 267B
std_regex.cpp 259B
steady_clock.cpp 136B
thread_safety_attributes.cpp 79B
add_person.dart 2KB
list_people.dart 1KB
add_person.go 3KB
go_benchmark_test.go 3KB
list_people_test.go 2KB
add_person_test.go 1KB
list_people.go 1KB
gtest.h 851KB
gmock.h 569KB
descriptor.pb.h 532KB
upb.h 300KB
upb.h 300KB
php-upb.h 204KB
GPBDictionary.h 187KB
gtest-type-util.h 182KB
gmock-matchers.h 162KB
repeated_field.h 99KB
type.pb.h 95KB
descriptor.h 94KB
gtest.h 93KB
btree.h 92KB
gmock-generated-actions.h 82KB
wire_format_lite.h 82KB
gtest-port.h 79KB
gtest-param-util-generated.h 78KB
extension_set.h 77KB
container.h 75KB
plugin.pb.h 75KB
gmock-spec-builders.h 73KB
coded_stream.h 68KB
raw_hash_set.h 67KB
map_test_util.h 67KB
json.h 66KB
test_util.h 62KB
message.h 61KB
protobuf.h 60KB
GPBArray.h 59KB
time.h 59KB
wrappers.pb.h 58KB
gtest-internal.h 55KB
variant.h 55KB
api.pb.h 55KB
gtest-internal-inl.h 46KB
gmock-generated-matchers.h 45KB
solver.h 45KB
message_differencer.h 44KB
conformance_archetype.h 44KB
map.h 44KB
mutex.h 42KB
struct.pb.h 41KB
benchmark.h 41KB
gmock-actions.h 41KB
map_type_handler.h 38KB
gmock-generated-function-mockers.h 38KB
strutil.h 38KB
cord.h 38KB
exception_safety_testing.h 37KB
hash.h 37KB
cpp_helpers.h 36KB
int128.h 35KB
gtest-printers.h 34KB
generated_message_table_driven_lite.h 33KB
variant.h 33KB
substitute.h 32KB
jet.h 32KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
火种源码
- 粉丝: 255
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功