我用C++编写了一些测试代码来处理CSV文件。在Windows和Linux上编译和运行相同的代码会产生显着不同的性能:
CSV 文件大小: ~30MB
Windows性能(在 Windows 8 上(:80秒
Linux 性能(在 Ubuntu 4.4.0-72 通用上(:2 秒
在此处输入代码
两个代码都在cmdline上运行。Linux 代码是使用 g++ 构建的,支持 C++11。Windows Code是使用Visual Studio 2017构建的。
起初,我认为 Linux 上的计时器实现是错误的,所以我自己对它们进行了计时,结果是一样的。
谁能给我一些提示,说明这种巨大的差异从何而来?当我在调试器中运行窗口时,我看到内存分配非常慢。但不确定这是否是主要原因。
代码和 CSV 已附上
#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <fstream>
#include <sstream>
#include <chrono>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <stdlib.h>
using namespace std;
class readCSV {
private:
vector<string> headers;
bool set_header = true;
string file_name;
unordered_map<string, vector<double>*> my_map;
unsigned int line_count = 1;
double duration;
public:
readCSV(string file, bool header)
:file_name(file), set_header(header), duration(0) {
parseCSV();
};
void parseCSV();
double find_duration() {
return duration / 1000.0;
}
vector<double> find_result(string names) {
vector <double> result;
unordered_map<string, vector<double>*>::iterator it = my_map.find(names);
if (it == my_map.end()) {
cout << "invalid query: " << names << endl;
exit;
}
cout << names << endl;
size_t size = it->second->size();
vector<double>* list = it->second;
double mean = 1.0 * accumulate(list->begin(), list->end(), 0.0) / size;
double sq_sum = inner_product(list->begin(), list->end(), list->begin(), 0.0);
double stdev = sqrt(sq_sum / size - mean * mean);
result.push_back(mean);
result.push_back(stdev);
return result;
}
~readCSV() {
//release memory to prevent leak
for (int i = 0; i < headers.size(); ++i) {
delete my_map.find(headers[i])->second;
}
}
};
void readCSV::parseCSV() {
ifstream file(file_name);
string line;
// log start time
long start = chrono::duration_cast<chrono::milliseconds>(
chrono::steady_clock::now().time_since_epoch()).count();
while (getline(file, line)) {
int count = 0;
string cell;
stringstream temp_line(line);
while (getline(temp_line, cell, ',')) {
unordered_map<string, vector<double>*>::iterator it;
if (line_count == 1) {
vector<double>* addr = new vector<double>();
if (set_header) {
cell.erase(remove(cell.begin(), cell.end(), '"'), cell.end());
headers.push_back(cell);
my_map.insert(make_pair(cell, addr));
}
else {
string head = to_string(count);
headers.push_back(head);
my_map.insert(make_pair(head, addr));
}
count++;
continue;
}
it = my_map.find(headers[count]);
if (it != my_map.end()) {
double num = atof(cell.c_str());
it->second->push_back(num);
}
else {
cout << "CSV file corrupted!" << endl;
return;
}
count++;
}
line_count++;
}
//log finish time
long stop = chrono::duration_cast<chrono::milliseconds>(
chrono::steady_clock::now().time_since_epoch()).count();
duration = stop - start;
}
int main(int argc, char **argv) {
if (argc < 3) {
cout << "wrong argument" << endl;
return -1;
}
string filename(argv[1]);
string feature(argv[2]);
readCSV file(filename, true);
vector<double> result = file.find_result(feature);
cout << "duration is " << file.find_duration() << " seconds" << endl;
cout << "Mean is " << result[0] << " " << "STDev is " << result[1] << endl;
return 0;
}
CSV 文件:
您可能正在调试模式下编译 VS 版本。将编辑器顶部的组合框从"调试"更改为"发布"并重新编译;您应该会看到性能显著提高。