Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
benchmark
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
benchmark
Commits
f2d1957d
Commit
f2d1957d
authored
Feb 11, 2014
by
Dominic Hamon
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9 from iiSeymour/patch-1
Update README.md
parents
6200a229
465cb09e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
35 deletions
+51
-35
README.md
README.md
+51
-35
No files found.
README.md
View file @
f2d1957d
...
...
@@ -10,40 +10,43 @@ Example usage:
Define a function that executes the code to be measured a
specified number of times:
static void BM_StringCreation(benchmark::State& state) {
```
c++
static
void
BM_StringCreation
(
benchmark
::
State
&
state
)
{
while
(
state
.
KeepRunning
())
std
::
string
empty_string
;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);
}
// Register the function as a benchmark
BENCHMARK
(
BM_StringCreation
);
// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
// Define another benchmark
static
void
BM_StringCopy
(
benchmark
::
State
&
state
)
{
std
::
string
x
=
"hello"
;
while
(
state
.
KeepRunning
())
std
::
string
copy
(
x
);
}
BENCHMARK(BM_StringCopy);
// Augment the main() program to invoke benchmarks if specified
// via the --benchmarks command line flag. E.g.,
// my_unittest --benchmark_filter=all
// my_unittest --benchmark_filter=BM_StringCreation
// my_unittest --benchmark_filter=String
// my_unittest --benchmark_filter='Copy|Creation'
int main(int argc, char** argv) {
}
BENCHMARK
(
BM_StringCopy
);
// Augment the main() program to invoke benchmarks if specified
// via the --benchmarks command line flag. E.g.,
// my_unittest --benchmark_filter=all
// my_unittest --benchmark_filter=BM_StringCreation
// my_unittest --benchmark_filter=String
// my_unittest --benchmark_filter='Copy|Creation'
int
main
(
int
argc
,
char
**
argv
)
{
benchmark
::
Initialize
(
&
argc
,
argv
);
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:
static void BM_memcpy(benchmark::State& state) {
```
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
());
while
(
state
.
KeepRunning
())
{
...
...
@@ -52,21 +55,25 @@ of memcpy() calls of different lengths:
int64_t
(
state
.
iterations
)
*
int64_t
(
state
.
range_x
()));
delete
[]
src
;
delete
[]
dst
;
}
BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
}
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.
BENCHMARK(BM_memcpy)->Range(8, 8<<10);
```
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.
static void BM_SetInsert(benchmark::State& state) {
```
c++
static
void
BM_SetInsert
(
benchmark
::
State
&
state
)
{
while
(
state
.
KeepRunning
())
{
state
.
PauseTiming
();
std
::
set
<
int
>
data
=
ConstructRandomSet
(
state
.
range_x
());
...
...
@@ -74,8 +81,8 @@ measuring the speed of set insertion.
for
(
int
j
=
0
;
j
<
state
.
rangeY
;
++
j
)
data
.
insert
(
RandomNumber
());
}
}
BENCHMARK(BM_SetInsert)
}
BENCHMARK
(
BM_SetInsert
)
->
ArgPair
(
1
<<
10
,
1
)
->
ArgPair
(
1
<<
10
,
8
)
->
ArgPair
(
1
<<
10
,
64
)
...
...
@@ -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.
BENCHMARK(BM_SetInsert)->RangePair(1<<10, 8<<10, 1, 512);
```
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,20 +108,23 @@ 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.
static benchmark::internal::Benchmark* CustomArguments(
```
c++
static
benchmark
::
internal
::
Benchmark
*
CustomArguments
(
benchmark
::
internal
::
Benchmark
*
b
)
{
for
(
int
i
=
0
;
i
<=
10
;
++
i
)
for
(
int
j
=
32
;
j
<=
1024
*
1024
;
j
*=
8
)
b
=
b
->
ArgPair
(
i
,
j
);
return
b
;
}
BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
}
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.
template <class Q> int BM_Sequential(benchmark::State& state) {
```
c++
template
<
class
Q
>
int
BM_Sequential
(
benchmark
::
State
&
state
)
{
Q
q
;
typename
Q
::
value_type
v
;
while
(
state
.
KeepRunning
())
{
...
...
@@ -123,15 +136,17 @@ Measures throughput in the absence of multiprogramming.
// actually messages, not bytes:
state
.
SetBytesProcessed
(
static_cast
<
int64_t
>
(
state
.
iterations
())
*
state
.
range_x
());
}
BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
}
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:
static void BM_MultiThreaded(benchmark::State& state) {
```
c++
static
void
BM_MultiThreaded
(
benchmark
::
State
&
state
)
{
if
(
state
.
thread_index
==
0
)
{
// Setup code here.
}
...
...
@@ -141,5 +156,6 @@ wrapped in a check against the thread index:
if
(
state
.
thread_index
==
0
)
{
// Teardown code here.
}
}
BENCHMARK(BM_MultiThreaded)->Threads(2);
}
BENCHMARK
(
BM_MultiThreaded
)
->
Threads
(
2
);
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment