mirror of
https://github.com/google/benchmark.git
synced 2025-04-29 06:20:32 +08:00
make DoNotOptimize work with rvalues and convert basic_test to use it.
This commit is contained in:
parent
f7d14e719b
commit
166334a284
@ -184,12 +184,12 @@ void UseCharPointer(char const volatile*);
|
||||
// See: http://stackoverflow.com/questions/28287064
|
||||
#if defined(__GNUC__)
|
||||
template <class Tp>
|
||||
inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
|
||||
inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
|
||||
asm volatile("" : "+r" (value));
|
||||
}
|
||||
#else
|
||||
template <class Tp>
|
||||
inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
|
||||
inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
|
||||
internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
|
||||
}
|
||||
#endif
|
||||
|
@ -8,8 +8,7 @@
|
||||
|
||||
void BM_empty(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
volatile std::size_t x = state.iterations();
|
||||
((void)x);
|
||||
benchmark::DoNotOptimize(state.iterations());
|
||||
}
|
||||
}
|
||||
BENCHMARK(BM_empty);
|
||||
@ -18,8 +17,7 @@ BENCHMARK(BM_empty)->ThreadPerCpu();
|
||||
void BM_spin_empty(benchmark::State& state) {
|
||||
while (state.KeepRunning()) {
|
||||
for (int x = 0; x < state.range_x(); ++x) {
|
||||
volatile int dummy = x;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -28,13 +26,11 @@ BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
|
||||
|
||||
void BM_spin_pause_before(benchmark::State& state) {
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
while(state.KeepRunning()) {
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -46,13 +42,11 @@ void BM_spin_pause_during(benchmark::State& state) {
|
||||
while(state.KeepRunning()) {
|
||||
state.PauseTiming();
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
state.ResumeTiming();
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,13 +75,11 @@ BENCHMARK(BM_pause_during_realtime)->ThreadPerCpu();
|
||||
void BM_spin_pause_after(benchmark::State& state) {
|
||||
while(state.KeepRunning()) {
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
BASIC_BENCHMARK_TEST(BM_spin_pause_after);
|
||||
@ -96,18 +88,15 @@ BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
|
||||
|
||||
void BM_spin_pause_before_and_after(benchmark::State& state) {
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
while(state.KeepRunning()) {
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < state.range_x(); ++i) {
|
||||
volatile int dummy = i;
|
||||
((void)dummy);
|
||||
benchmark::DoNotOptimize(i);
|
||||
}
|
||||
}
|
||||
BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after);
|
||||
|
Loading…
Reference in New Issue
Block a user