| 10 | #include <numeric> |
| 11 | |
| 12 | class DataProcessor { |
| 13 | private: |
| 14 | std::vector<std::vector<double>> data; |
| 15 | int rows; |
| 16 | int cols; |
| 17 | |
| 18 | public: |
| 19 | DataProcessor() : rows(0), cols(0) {} |
| 20 | |
| 21 | bool loadCSV(const std::string& filename) { |
| 22 | std::ifstream file(filename); |
| 23 | if (!file.is_open()) { |
| 24 | std::cerr << "Error: Cannot open file " << filename << std::endl; |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | std::string line; |
| 29 | while (std::getline(file, line)) { |
| 30 | std::vector<double> row; |
| 31 | std::stringstream ss(line); |
| 32 | std::string cell; |
| 33 | |
| 34 | while (std::getline(ss, cell, ',')) { |
| 35 | try { |
| 36 | row.push_back(std::stod(cell)); |
| 37 | } catch (...) { |
| 38 | row.push_back(0.0); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (!row.empty()) { |
| 43 | data.push_back(row); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | rows = data.size(); |
| 48 | cols = rows > 0 ? data[0].size() : 0; |
| 49 | |
| 50 | std::cout << "Loaded " << rows << " rows and " << cols << " columns" << std::endl; |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | // PERFORMANCE ISSUE: This function iterates in column-major order |
| 55 | // but C++ stores 2D vectors in row-major order. |
| 56 | // This causes poor cache utilization and excessive cache misses. |
| 57 | std::vector<double> computeColumnSums() { |
| 58 | std::vector<double> sums(cols, 0.0); |
| 59 | |
| 60 | // BAD: Column-major iteration - causes cache misses |
| 61 | // The outer loop iterates over columns, inner loop over rows |
| 62 | // This means we jump around in memory instead of accessing contiguously |
| 63 | for (int j = 0; j < cols; ++j) { |
| 64 | for (int i = 0; i < rows; ++i) { |
| 65 | sums[j] += data[i][j]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return sums; |
nothing calls this directly
no outgoing calls
no test coverage detected