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

标签 c++ linux windows performance

我用 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/

相关文章:

c++ - 仅为从某种语言编译的对象添加链接库?

c++ - SDL2 程序仅在使用 SDL_RENDERER_SOFTWARE 创建渲染器时才有效

c++ - 在 Visual Studio 2015 中使用 XP Targeting 编译时 Windows XP 上出现 fatal error

c++ - 独立 exe : Where on disk to save config files and logs?

c++ - 在 C++ 中返回运行时可用虚拟内存的大小

c++ - 为什么我应该避免在 C++ 中使用 malloc?

c++ - 使用 glTexCoordPointer() 的问题

linux - 使用 sed 在 linux 中编辑文本文件

java - 如何在 Linux 中从/proc/swaps 获取交换大小?

linux - 撇号 : Image Manager hanging on replicated site