mirror of
https://github.com/google/benchmark.git
synced 2025-03-28 21:10:58 +08:00
Update README.md
Switched to language specific markdown for code sections to provide syntax highlighting when rendered.
This commit is contained in:
parent
0a1593b06f
commit
465cb09eb6
18
README.md
18
README.md
@ -10,6 +10,7 @@ Example usage:
|
||||
Define a function that executes the code to be measured a
|
||||
specified number of times:
|
||||
|
||||
```c++
|
||||
static void BM_StringCreation(benchmark::State& state) {
|
||||
while (state.KeepRunning())
|
||||
std::string empty_string;
|
||||
@ -36,13 +37,15 @@ specified number of times:
|
||||
benchmark::RunSpecifiedBenchmarks();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Sometimes a family of microbenchmarks 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 microbenchmarks for measuring the speed
|
||||
of memcpy() calls of different lengths:
|
||||
of `memcpy()` calls of different lengths:
|
||||
|
||||
```c++
|
||||
static void BM_memcpy(benchmark::State& state) {
|
||||
char* src = new char[state.range_x()]; char* dst = new char[state.range_x()];
|
||||
memset(src, 'x', state.range_x());
|
||||
@ -54,18 +57,22 @@ of memcpy() calls of different lengths:
|
||||
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
|
||||
microbenchmark for each such argument.
|
||||
|
||||
```c++
|
||||
BENCHMARK(BM_memcpy)->Range(8, 8<<10);
|
||||
```
|
||||
|
||||
You might have a microbenchmark that depends on two inputs. For
|
||||
example, the following code defines a family of microbenchmarks for
|
||||
measuring the speed of set insertion.
|
||||
|
||||
```c++
|
||||
static void BM_SetInsert(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
state.PauseTiming();
|
||||
@ -84,13 +91,16 @@ measuring the speed of set insertion.
|
||||
->ArgPair(8<<10, 8)
|
||||
->ArgPair(8<<10, 64)
|
||||
->ArgPair(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 microbenchmark for each such pair.
|
||||
|
||||
```c++
|
||||
BENCHMARK(BM_SetInsert)->RangePair(1<<10, 8<<10, 1, 512);
|
||||
```
|
||||
|
||||
For more complex patterns of inputs, passing a custom function
|
||||
to Apply allows programmatic specification of an
|
||||
@ -98,6 +108,7 @@ arbitrary set of arguments to run the microbenchmark on.
|
||||
The following example enumerates a dense range on one parameter,
|
||||
and a sparse range on the second.
|
||||
|
||||
```c++
|
||||
static benchmark::internal::Benchmark* CustomArguments(
|
||||
benchmark::internal::Benchmark* b) {
|
||||
for (int i = 0; i <= 10; ++i)
|
||||
@ -106,11 +117,13 @@ and a sparse range on the second.
|
||||
return b;
|
||||
}
|
||||
BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
|
||||
```
|
||||
|
||||
Templated microbenchmarks work the same way:
|
||||
Produce then consume 'size' messages 'iters' times
|
||||
Measures throughput in the absence of multiprogramming.
|
||||
|
||||
```c++
|
||||
template <class Q> int BM_Sequential(benchmark::State& state) {
|
||||
Q q;
|
||||
typename Q::value_type v;
|
||||
@ -125,12 +138,14 @@ Measures throughput in the absence of multiprogramming.
|
||||
static_cast<int64_t>(state.iterations())*state.range_x());
|
||||
}
|
||||
BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
|
||||
```
|
||||
|
||||
In a multithreaded test, it is guaranteed that none of the threads will start
|
||||
until all have called KeepRunning, and all will have finished before KeepRunning
|
||||
returns false. As such, any global setup or teardown you want to do can be
|
||||
wrapped in a check against the thread index:
|
||||
|
||||
```c++
|
||||
static void BM_MultiThreaded(benchmark::State& state) {
|
||||
if (state.thread_index == 0) {
|
||||
// Setup code here.
|
||||
@ -143,3 +158,4 @@ wrapped in a check against the thread index:
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_MultiThreaded)->Threads(2);
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user