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
e428b9ee
Commit
e428b9ee
authored
Mar 27, 2015
by
Eric Fiselier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add 'benchmark::DoNotOptimize(...)' to help users prevent optimizations
parent
0a0bb8fe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
31 deletions
+50
-31
README.md
README.md
+13
-0
benchmark_api.h
include/benchmark/benchmark_api.h
+19
-0
benchmark.cc
src/benchmark.cc
+2
-0
basic_test.cc
test/basic_test.cc
+11
-22
benchmark_test.cc
test/benchmark_test.cc
+5
-9
No files found.
README.md
View file @
e428b9ee
...
@@ -160,6 +160,19 @@ static void BM_MultiThreaded(benchmark::State& state) {
...
@@ -160,6 +160,19 @@ static void BM_MultiThreaded(benchmark::State& state) {
}
}
}
}
BENCHMARK
(
BM_MultiThreaded
)
->
Threads
(
2
);
BENCHMARK
(
BM_MultiThreaded
)
->
Threads
(
2
);
To
prevent
a
value
or
expression
from
being
optimized
away
by
the
compiler
the
`
benchmark
::
DoNotOptimize
(...)
`
function
can
be
used
.
```
c++
static void BM_test(benchmark::State& state) {
while (state.KeepRunning()) {
int x = 0;
for (int i=0; i < 64; ++i) {
benchmark::DoNotOptimize(x += i);
}
}
}
```
```
...
...
include/benchmark/benchmark_api.h
View file @
e428b9ee
...
@@ -174,8 +174,27 @@ struct EnableIfString<T, typename Voider<typename T::basic_string>::type> {
...
@@ -174,8 +174,27 @@ struct EnableIfString<T, typename Voider<typename T::basic_string>::type> {
typedef
int
type
;
typedef
int
type
;
};
};
void
UseCharPointer
(
char
const
volatile
*
);
}
// end namespace internal
}
// end namespace internal
// The DoNotOptimize(...) function can be used to prevent a value or
// expression from being optimized away by the compiler. This function is
// intented to add little to no overhead.
// See: http://stackoverflow.com/questions/28287064
#if defined(__GNUC__)
template
<
class
Tp
>
inline
BENCHMARK_ALWAYS_INLINE
void
DoNotOptimize
(
Tp
const
&
value
)
{
asm
volatile
(
""
:
"+r"
(
const_cast
<
Tp
&>
(
value
)));
}
#else
template
<
class
Tp
>
inline
BENCHMARK_ALWAYS_INLINE
void
DoNotOptimize
(
Tp
const
&
value
)
{
internal
::
UseCharPointer
(
&
reinterpret_cast
<
char
const
volatile
&>
(
value
));
}
#endif
// State is passed to a running Benchmark and contains state for the
// State is passed to a running Benchmark and contains state for the
// benchmark to use.
// benchmark to use.
class
State
{
class
State
{
...
...
src/benchmark.cc
View file @
e428b9ee
...
@@ -73,6 +73,8 @@ namespace benchmark {
...
@@ -73,6 +73,8 @@ namespace benchmark {
namespace
internal
{
namespace
internal
{
void
UseCharPointer
(
char
const
volatile
*
)
{}
// NOTE: This is a dummy "mutex" type used to denote the actual mutex
// NOTE: This is a dummy "mutex" type used to denote the actual mutex
// returned by GetBenchmarkLock(). This is only used to placate the thread
// returned by GetBenchmarkLock(). This is only used to placate the thread
// safety warnings by giving the return of GetBenchmarkLock() a name.
// safety warnings by giving the return of GetBenchmarkLock() a name.
...
...
test/basic_test.cc
View file @
e428b9ee
...
@@ -8,8 +8,7 @@
...
@@ -8,8 +8,7 @@
void
BM_empty
(
benchmark
::
State
&
state
)
{
void
BM_empty
(
benchmark
::
State
&
state
)
{
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
volatile
std
::
size_t
x
=
state
.
iterations
();
benchmark
::
DoNotOptimize
(
state
.
iterations
());
((
void
)
x
);
}
}
}
}
BENCHMARK
(
BM_empty
);
BENCHMARK
(
BM_empty
);
...
@@ -18,8 +17,7 @@ BENCHMARK(BM_empty)->ThreadPerCpu();
...
@@ -18,8 +17,7 @@ BENCHMARK(BM_empty)->ThreadPerCpu();
void
BM_spin_empty
(
benchmark
::
State
&
state
)
{
void
BM_spin_empty
(
benchmark
::
State
&
state
)
{
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
for
(
int
x
=
0
;
x
<
state
.
range_x
();
++
x
)
{
for
(
int
x
=
0
;
x
<
state
.
range_x
();
++
x
)
{
volatile
int
dummy
=
x
;
benchmark
::
DoNotOptimize
(
x
);
((
void
)
dummy
);
}
}
}
}
}
}
...
@@ -28,13 +26,11 @@ BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
...
@@ -28,13 +26,11 @@ BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
void
BM_spin_pause_before
(
benchmark
::
State
&
state
)
{
void
BM_spin_pause_before
(
benchmark
::
State
&
state
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
}
}
...
@@ -46,13 +42,11 @@ void BM_spin_pause_during(benchmark::State& state) {
...
@@ -46,13 +42,11 @@ void BM_spin_pause_during(benchmark::State& state) {
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
state
.
PauseTiming
();
state
.
PauseTiming
();
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
state
.
ResumeTiming
();
state
.
ResumeTiming
();
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
}
}
...
@@ -81,13 +75,11 @@ BENCHMARK(BM_pause_during_realtime)->ThreadPerCpu();
...
@@ -81,13 +75,11 @@ BENCHMARK(BM_pause_during_realtime)->ThreadPerCpu();
void
BM_spin_pause_after
(
benchmark
::
State
&
state
)
{
void
BM_spin_pause_after
(
benchmark
::
State
&
state
)
{
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
BASIC_BENCHMARK_TEST
(
BM_spin_pause_after
);
BASIC_BENCHMARK_TEST
(
BM_spin_pause_after
);
...
@@ -96,18 +88,15 @@ BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
...
@@ -96,18 +88,15 @@ BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
void
BM_spin_pause_before_and_after
(
benchmark
::
State
&
state
)
{
void
BM_spin_pause_before_and_after
(
benchmark
::
State
&
state
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
{
volatile
int
dummy
=
i
;
benchmark
::
DoNotOptimize
(
i
);
((
void
)
dummy
);
}
}
}
}
BASIC_BENCHMARK_TEST
(
BM_spin_pause_before_and_after
);
BASIC_BENCHMARK_TEST
(
BM_spin_pause_before_and_after
);
...
...
test/benchmark_test.cc
View file @
e428b9ee
...
@@ -84,9 +84,8 @@ BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
...
@@ -84,9 +84,8 @@ BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
static
void
BM_CalculatePi
(
benchmark
::
State
&
state
)
{
static
void
BM_CalculatePi
(
benchmark
::
State
&
state
)
{
static
const
int
depth
=
1024
;
static
const
int
depth
=
1024
;
double
pi
BENCHMARK_UNUSED
=
0.0
;
while
(
state
.
KeepRunning
())
{
while
(
state
.
KeepRunning
())
{
pi
=
CalculatePi
(
depth
);
benchmark
::
DoNotOptimize
(
CalculatePi
(
depth
)
);
}
}
}
}
BENCHMARK
(
BM_CalculatePi
)
->
Threads
(
8
);
BENCHMARK
(
BM_CalculatePi
)
->
Threads
(
8
);
...
@@ -129,11 +128,8 @@ BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
...
@@ -129,11 +128,8 @@ BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
static
void
BM_StringCompare
(
benchmark
::
State
&
state
)
{
static
void
BM_StringCompare
(
benchmark
::
State
&
state
)
{
std
::
string
s1
(
state
.
range_x
(),
'-'
);
std
::
string
s1
(
state
.
range_x
(),
'-'
);
std
::
string
s2
(
state
.
range_x
(),
'-'
);
std
::
string
s2
(
state
.
range_x
(),
'-'
);
int
r
=
0
;
while
(
state
.
KeepRunning
())
while
(
state
.
KeepRunning
())
r
|=
s1
.
compare
(
s2
);
benchmark
::
DoNotOptimize
(
s1
.
compare
(
s2
));
// Prevent compiler optimizations
assert
(
r
!=
std
::
numeric_limits
<
int
>::
max
());
}
}
BENCHMARK
(
BM_StringCompare
)
->
Range
(
1
,
1
<<
20
);
BENCHMARK
(
BM_StringCompare
)
->
Range
(
1
,
1
<<
20
);
...
@@ -159,10 +155,10 @@ BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
...
@@ -159,10 +155,10 @@ BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
static
void
BM_LongTest
(
benchmark
::
State
&
state
)
{
static
void
BM_LongTest
(
benchmark
::
State
&
state
)
{
double
tracker
=
0.0
;
double
tracker
=
0.0
;
while
(
state
.
KeepRunning
())
while
(
state
.
KeepRunning
())
{
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
for
(
int
i
=
0
;
i
<
state
.
range_x
();
++
i
)
tracker
+=
i
;
benchmark
::
DoNotOptimize
(
tracker
+=
i
)
;
assert
(
tracker
>
1.0
);
}
}
}
BENCHMARK
(
BM_LongTest
)
->
Range
(
1
<<
16
,
1
<<
28
);
BENCHMARK
(
BM_LongTest
)
->
Range
(
1
<<
16
,
1
<<
28
);
...
...
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