Skip to content

Commit c562760

Browse files
authored
Merge 01d768b into 0ed4456
2 parents 0ed4456 + 01d768b commit c562760

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

include/benchmark/benchmark_api.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,17 @@ class State {
425425

426426
// Range arguments for this run. CHECKs if the argument has been set.
427427
BENCHMARK_ALWAYS_INLINE
428-
int range(std::size_t pos) const {
428+
int range(std::size_t pos = 0) const {
429429
assert(range_.size() > pos);
430430
return range_[pos];
431431
}
432432

433+
BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead")
434+
int range_x() const { return range(0); }
435+
436+
BENCHMARK_DEPRECATED_MSG("use 'range(1)' instead")
437+
int range_y() const { return range(1); }
438+
433439
BENCHMARK_ALWAYS_INLINE
434440
size_t iterations() const { return total_iterations_; }
435441

@@ -498,11 +504,31 @@ class Benchmark {
498504
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
499505
Benchmark* Args(const std::vector<int>& args);
500506

507+
// Equivalent to Args({x, y})
508+
// NOTE: This is a legacy C++03 interface provided for compatibility only.
509+
// New code should use 'Args'.
510+
Benchmark* ArgPair(int x, int y) {
511+
std::vector<int> args;
512+
args.push_back(x);
513+
args.push_back(y);
514+
return Args(args);
515+
}
516+
501517
// Run this benchmark once for a number of values picked from the
502518
// ranges [start..limit]. (starts and limits are always picked.)
503519
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
504520
Benchmark* Ranges(const std::vector<std::pair<int, int> >& ranges);
505521

522+
// Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}).
523+
// NOTE: This is a legacy C++03 interface provided for compatibility only.
524+
// New code should use 'Ranges'.
525+
Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2) {
526+
std::vector<std::pair<int, int> > ranges;
527+
ranges.push_back(std::make_pair(lo1, hi1));
528+
ranges.push_back(std::make_pair(lo2, hi2));
529+
return Ranges(ranges);
530+
}
531+
506532
// Pass this benchmark object to *func, which can customize
507533
// the benchmark by calling various methods like Arg, Args,
508534
// Threads, etc.

include/benchmark/macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353

5454
#if defined(__GNUC__)
5555
# define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
56+
# define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
5657
#else
5758
# define BENCHMARK_BUILTIN_EXPECT(x, y) x
59+
# define BENCHMARK_DEPRECATED_MSG(msg)
5860
#endif
5961

6062
#if defined(__GNUC__) && !defined(__clang__)

test/cxx03_test.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
1+
#undef NDEBUG
22
#include <cstddef>
3+
#include <cassert>
34

45
#include "benchmark/benchmark.h"
56

@@ -15,6 +16,16 @@ void BM_empty(benchmark::State& state) {
1516
}
1617
BENCHMARK(BM_empty);
1718

19+
// The new C++11 interface for args/ranges requires initializer list support.
20+
// Therefore we provide the old interface to support C++03.
21+
void BM_old_arg_range_interface(benchmark::State& state) {
22+
assert((state.range(0) == 1 && state.range(1) == 2) ||
23+
(state.range(0) == 5 && state.range(1) == 6));
24+
while (state.KeepRunning()) {
25+
}
26+
}
27+
BENCHMARK(BM_old_arg_range_interface)->ArgPair(1, 2)->RangePair(5, 5, 6, 6);
28+
1829
template <class T, class U>
1930
void BM_template2(benchmark::State& state) {
2031
BM_empty(state);

test/multiple_ranges_test.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <set>
44
#include <cassert>
5+
#include <limits>
56

67
class MultipleRangesFixture : public ::benchmark::Fixture {
78
public:
@@ -41,6 +42,15 @@ BENCHMARK_DEFINE_F(MultipleRangesFixture, Empty)(benchmark::State& state) {
4142
}
4243
}
4344

44-
BENCHMARK_REGISTER_F(MultipleRangesFixture, Empty)->RangeMultiplier(2)->Ranges({{1, 2}, {3, 7}, {5, 15}})->Args({7, 6, 3});
45+
BENCHMARK_REGISTER_F(MultipleRangesFixture, Empty)->RangeMultiplier(2)
46+
->Ranges({{1, 2}, {3, 7}, {5, 15}})->Args({7, 6, 3});
47+
48+
void BM_CheckDefaultArgument(benchmark::State& state) {
49+
// Test that the 'range()' without an argument is the same as 'range(0)'.
50+
assert(state.range() == state.range(0));
51+
assert(state.range() != state.range(1));
52+
while (state.KeepRunning()) {}
53+
}
54+
BENCHMARK(BM_CheckDefaultArgument)->Ranges({{1, 5}, {6, 10}});
4555

4656
BENCHMARK_MAIN()

0 commit comments

Comments
 (0)