Linux vs Windows,C ++运行时性能差异超过读取CSV文件

时间:2017-04-14 03:07:26

标签: 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代码是使用Visual Studio 2017构建的。

起初,我认为Linux上的计时器实现是错误的,所以我自己计时,结果是一样的。

有人能给我一些暗示这个巨大差异来自哪里?当我在调试器中运行windows时,我看到内存分配非常慢。但不确定这是否是主要原因。

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

1 个答案:

答案 0 :(得分:0)

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