Skip to content

Commit 5056098

Browse files
LebedevRIdmah42
andauthored
[NFC] complexity_n is not of IterationCount type (google#1709)
There is no bug here, but it gave me a scare the other day. It is not incorrect to use `IterationCount` here, since it's just an `int64_t` either way, but it's wildly confusing. Let's not do that. Co-authored-by: dominic <[email protected]>
1 parent 68689bf commit 5056098

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

include/benchmark/benchmark.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,15 @@ typedef std::map<std::string, Counter> UserCounters;
672672
// calculated automatically to the best fit.
673673
enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
674674

675+
typedef int64_t ComplexityN;
676+
675677
typedef int64_t IterationCount;
676678

677679
enum StatisticUnit { kTime, kPercentage };
678680

679681
// BigOFunc is passed to a benchmark in order to specify the asymptotic
680682
// computational complexity for the benchmark.
681-
typedef double(BigOFunc)(IterationCount);
683+
typedef double(BigOFunc)(ComplexityN);
682684

683685
// StatisticsFunc is passed to a benchmark in order to compute some descriptive
684686
// statistics over all the measurements of some type
@@ -875,10 +877,12 @@ class BENCHMARK_EXPORT State {
875877
// and complexity_n will
876878
// represent the length of N.
877879
BENCHMARK_ALWAYS_INLINE
878-
void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }
880+
void SetComplexityN(ComplexityN complexity_n) {
881+
complexity_n_ = complexity_n;
882+
}
879883

880884
BENCHMARK_ALWAYS_INLINE
881-
int64_t complexity_length_n() const { return complexity_n_; }
885+
ComplexityN complexity_length_n() const { return complexity_n_; }
882886

883887
// If this routine is called with items > 0, then an items/s
884888
// label is printed on the benchmark report line for the currently
@@ -967,7 +971,7 @@ class BENCHMARK_EXPORT State {
967971
// items we don't need on the first cache line
968972
std::vector<int64_t> range_;
969973

970-
int64_t complexity_n_;
974+
ComplexityN complexity_n_;
971975

972976
public:
973977
// Container for user-defined counters.
@@ -1805,7 +1809,7 @@ class BENCHMARK_EXPORT BenchmarkReporter {
18051809
// Keep track of arguments to compute asymptotic complexity
18061810
BigO complexity;
18071811
BigOFunc* complexity_lambda;
1808-
int64_t complexity_n;
1812+
ComplexityN complexity_n;
18091813

18101814
// what statistics to compute from the measurements
18111815
const std::vector<internal::Statistics>* statistics;

src/complexity.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ std::string GetBigOString(BigO complexity) {
7777
// given by the lambda expression.
7878
// - n : Vector containing the size of the benchmark tests.
7979
// - time : Vector containing the times for the benchmark tests.
80-
// - fitting_curve : lambda expression (e.g. [](int64_t n) {return n; };).
80+
// - fitting_curve : lambda expression (e.g. [](ComplexityN n) {return n; };).
8181

8282
// For a deeper explanation on the algorithm logic, please refer to
8383
// https://en.wikipedia.org/wiki/Least_squares#Least_squares,_regression_analysis_and_statistics
8484

85-
LeastSq MinimalLeastSq(const std::vector<int64_t>& n,
85+
LeastSq MinimalLeastSq(const std::vector<ComplexityN>& n,
8686
const std::vector<double>& time,
8787
BigOFunc* fitting_curve) {
8888
double sigma_gn_squared = 0.0;
@@ -124,7 +124,7 @@ LeastSq MinimalLeastSq(const std::vector<int64_t>& n,
124124
// - complexity : If different than oAuto, the fitting curve will stick to
125125
// this one. If it is oAuto, it will be calculated the best
126126
// fitting curve.
127-
LeastSq MinimalLeastSq(const std::vector<int64_t>& n,
127+
LeastSq MinimalLeastSq(const std::vector<ComplexityN>& n,
128128
const std::vector<double>& time, const BigO complexity) {
129129
BM_CHECK_EQ(n.size(), time.size());
130130
BM_CHECK_GE(n.size(), 2); // Do not compute fitting curve is less than two
@@ -164,7 +164,7 @@ std::vector<BenchmarkReporter::Run> ComputeBigO(
164164
if (reports.size() < 2) return results;
165165

166166
// Accumulators.
167-
std::vector<int64_t> n;
167+
std::vector<ComplexityN> n;
168168
std::vector<double> real_time;
169169
std::vector<double> cpu_time;
170170

0 commit comments

Comments
 (0)