Skip to content

Commit 43f4b6b

Browse files
committed
[Perf] Optimize memory usage by reserve the capacity of vector manually.
1 parent 37dfaef commit 43f4b6b

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

include/sample_entropy_calculator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class SampleEntropyCalculator {
8787
protected:
8888
virtual void _ComputeSampleEntropy() = 0;
8989
virtual std::string _Method() const = 0;
90-
const vector<T> _data;
90+
const vector<T> &_data;
9191
const T _r;
9292
unsigned K;
9393
const unsigned _n;

include/utils.h

+24-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
MSG(stderr, "ERROR", "File: %s, line: %d\n", __FILE__, __LINE__); \
3737
exit((error_code));
3838

39+
#define MSG_WARNING(error_code, fmt, ...) \
40+
MSG(stderr, "WARNING", (fmt), ##__VA_ARGS__); \
41+
MSG(stderr, "WARNING", "File: %s, line: %d\n", __FILE__, __LINE__);
42+
3943
#ifdef DEBUG
4044
#define MSG_DEBUG(fmt, ...) MSG(stdout, "DEBUG", (fmt), ##__VA_ARGS__);
4145
#else
@@ -83,8 +87,8 @@ double ComputeSampen(double A, double B, unsigned N, unsigned m);
8387
/**
8488
* @brief Read data from file.
8589
*
86-
* @param filename: The name of the file to read.
87-
* @return Read data.
90+
* @param[out] result where to store the read result
91+
* @param filename the name of the file to read
8892
*/
8993
template <typename T>
9094
void ReadData(std::vector<T> &result, std::string filename,
@@ -317,34 +321,42 @@ void ReadData(std::vector<T> &result, std::string filename,
317321
exit(-1);
318322
}
319323
unsigned org_n = n;
320-
if (n == 0)
321-
n = std::numeric_limits<unsigned>::max();
324+
if (n == 0) {
325+
n = 1024 * 1024;
326+
} else {
327+
result.reserve(n);
328+
result.resize(n);
329+
}
322330
unsigned count = 0;
331+
unsigned result_i = 0;
323332
T x = 0;
324333
if (input_type == "simple") {
325334
while (count < n + line_offset && ifs >> x) {
326-
if (count >= line_offset)
327-
result.push_back(x);
335+
if (count >= line_offset) {
336+
result[result_i] = x;
337+
++result_i;
338+
}
328339
++count;
329340
}
330341
} else if (input_type == "multirecord") {
331342
std::string line;
332343
while (count < n + line_offset && std::getline(ifs, line)) {
333344
std::istringstream iss(line);
334345
if (!(iss >> x >> x)) {
335-
std::cerr << "Input file foramt error. \n";
336-
exit(-1);
346+
MSG_ERROR(-1, "Input file foramt error.\n");
347+
}
348+
if (count >= line_offset) {
349+
result[result_i] = x;
350+
++result_i;
337351
}
338-
if (count >= line_offset)
339-
result.push_back(x);
340352
++count;
341353
}
342354
} else {
343355
MSG_ERROR(-1, "Invalid input-type: %s\n", input_type.c_str());
344356
}
345357
if (org_n && org_n != result.size()) {
346-
MSG_ERROR(-1, "Cannot read %u element from file %s. Only %zu read.\n",
347-
org_n, filename.c_str(), result.size());
358+
MSG_WARNING(-1, "Cannot read %u element from file %s. Only %zu read.\n",
359+
org_n, filename.c_str(), result.size());
348360
}
349361
ifs.close();
350362
}

src/fast_sampen.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ template <typename T> void SampleEntropyN0N1() {
284284
arg.line_offset);
285285
unsigned n = data.size();
286286
if (n <= K) {
287-
std::cerr << "Data length n " << n << " is too short (K = " << K;
288-
std::cerr << "). \n";
289-
exit(-1);
287+
MSG_ERROR(-1, "Data length n %d is to short (K = %d).\n", n, K);
290288
}
291289

292290
double var = ComputeVariance(data);

0 commit comments

Comments
 (0)