gpt4 book ai didi

c++ - Linux 与 Windows,C++ 运行时读取 CSV 文件的性能差异

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:39 24 4
gpt4 key购买 nike

我用 C++ 编写了一些测试代码来处理 CSV 文件。在 Windows 和 Linux 上编译和运行相同的代码会产生显着的性能差异:

CSV 文件大小:~30MB

Windows 性能(在 Windows 8 上):80 秒

Linux 性能(在 Ubuntu 4.4.0-72-generic 上):2秒

在此输入验证码

两个代码都在 cmdline 上运行。 Linux 代码是使用支持 C++11 的 g++ 构建的。 Windows Code 是使用 Visual Studio 2017 构建的。

一开始以为是linux下timer的实现不对,于是自己计时,结果一样。

谁能给我一些提示,说明这种巨大差异从何而来?当我在调试器中运行窗口时,我看到内存分配非常慢。但不确定这是否是主要原因。

附上代码和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 文件:

最佳答案

您可能正在 Debug模式下编译 VS 版本。将编辑器顶部的组合框从“Debug”更改为“Release”并重新编译;您应该会看到性能显着提高。

关于c++ - Linux 与 Windows,C++ 运行时读取 CSV 文件的性能差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43404634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com