C++ c++;vs MATLAB优化速度。例如,我的matlab代码比c++;?

C++ c++;vs MATLAB优化速度。例如,我的matlab代码比c++;?,c++,performance,matlab,optimization,C++,Performance,Matlab,Optimization,我写了一个程序,读取带有单元格垂直坐标的ascii文件,然后输出带有单元格中心的文件。有什么方法可以加快C++代码,至少匹配Matlab的性能。IM使用Puxy Buffic向量在我的C++代码,但我不知道我将如何实现类似数组?? 我正试图使用sleepy进行评测,但是在设置程序之前,如何才能开始评测呢?它只能运行已经运行的程序,而且输出非常混乱,不要转到我的代码行,而是像std::\u Vector\u cal>这样的行。。。。等等。“独占”和“包含”之间有什么区别?最高的%独占名称是“运算符

我写了一个程序,读取带有单元格垂直坐标的ascii文件,然后输出带有单元格中心的文件。有什么方法可以加快C++代码,至少匹配Matlab的性能。IM使用Puxy Buffic向量在我的C++代码,但我不知道我将如何实现类似数组?? 我正试图使用sleepy进行评测,但是在设置程序之前,如何才能开始评测呢?它只能运行已经运行的程序,而且输出非常混乱,不要转到我的代码行,而是像std::\u Vector\u cal>这样的行。。。。等等。“独占”和“包含”之间有什么区别?最高的%独占名称是“运算符删除”,第二个是“运算符新建”

我的C++源:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
    std::vector<double> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    return ret;
}

std::vector<int> GetValues_c(const std::vector<std::string>& src, int start, int end)
{
    std::vector<int> ret;
    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::atoi(src[i].c_str()));
    }
    return ret;
}

std::vector<double> polycentre(const std::vector<double>&  x,const std::vector<double>&  y,size_t ID)
{
    std::vector<double> C(3, 0);
    std::vector<double> x1(x.size(),0);
    std::vector<double> y1(y.size(),0);
    int sizx = x.size();
    int sizy = y.size();
    if(sizy != sizx)
    {
        std::cerr << "polycentre inputs not equal length";
    }
    double x0 = x[0];
    double y0 = y[0];
    for(int aa = 1; aa < sizx; ++aa)
    {
        if(x[aa] < x0)
        {
            x0 = x[aa];
        }
        if(y[aa] < y0)
        {
            y0 = y[aa];
        }
    }
    double A = 0.0;
    double B1 = 0.0;
    double B2 = 0.0;
    for(int aa = 0; aa < sizx; ++aa)
    {
        x1[aa] = x[aa] - x0;
        y1[aa] = y[aa] - y0;
    }
    for(int aa = 0; aa < sizx; ++aa)
    {
        if(aa != sizx-1)
        {
            A = A + (x1[aa]*y1[aa+1] - x1[aa+1]*y1[aa]);
            B1 = B1 + ((x1[aa]+x1[aa+1])*(x1[aa]*y1[aa+1]-x1[aa+1]*y1[aa]));
            B2 = B2 + ((y1[aa]+y1[aa+1])*(x1[aa]*y1[aa+1]-x1[aa+1]*y1[aa]));
        }
        else if(aa == sizx-1)
        {
            A = A + (x1[aa]*y1[0] - x1[0]*y1[aa]);
            B1 = B1 + ((x1[aa]+x1[0])*(x1[aa]*y1[0]-x1[0]*y1[aa]));
            B2 = B2 + ((y1[aa]+y1[0])*(x1[aa]*y1[0]-x1[0]*y1[aa]));
        }
    }
    A = A*0.5;
    C[0] = ID;
    C[1] = (((1/6.0)/A)*B1) + x0;
    C[2] = (((1/6.0)/A)*B2) + y0;
    return C;
}

template <typename T>

void PrintValues(const std::string& title, std::vector<std::vector<T>>& v, std::ofstream& outfil)
{
    if(outfil.is_open())
    {
        outfil << "ID,X,Y,Z \n";
        std::cout << title << std::endl;
        for(size_t line = 0; line < v.size(); ++line)
        {
            for(size_t val = 0; val < v[line].size(); ++val)
            {
                //std::cout << v[line][val] << " ";
                outfil.precision(10); 
                outfil << v[line][val] << ",";
            }
            outfil << "\n";
            //std::cout << std::endl;
        }
        //std::cout << std::endl;
    }
}

int main(int argc, char* argv[])
{

    std::ofstream outfil;

    if (argc < 2)
    {
        std::cerr << argv[0] << " needs to get input file (2dm)" << std::endl;
    }

    else if (argc == 3)
    {
        outfil.open(argv[2]);
    }

    else
    {
        outfil.open(std::string(argv[1]) + ".csv");
    }

    std::vector<std::vector<std::string>> values;
    std::ifstream fin(argv[1]);

    for (std::string line; std::getline(fin, line); )
    {
        std::istringstream in(line);
        values.push_back(
                         std::vector<std::string>(std::istream_iterator<std::string>(in),
                                                  std::istream_iterator<std::string>()));
    }

    std::vector<std::vector<int>> cells;
    std::vector<std::vector<double>> nodes;

    for (size_t i = 0; i < values.size(); ++i)
    {
        if(values[i][0] == "E3T")
        {
            cells.push_back(GetValues_c(values[i], 1, 5));
        }
        else if(values[i][0] == "E4Q")
        {
            cells.push_back(GetValues_c(values[i], 1, 6));
        }
        else if(values[i][0] == "ND")
        {
            nodes.push_back(GetValues_n(values[i], 1, 4));
        }
    }

    std::vector<std::vector<double>> cell_centres;

    for (size_t aa = 0; aa < cells.size(); ++aa)
    {
        if(cells[aa].size() == 5)
        {
            std::vector<double> xs;
            xs.push_back(nodes[cells[aa][1] - 1][1]);
            xs.push_back(nodes[cells[aa][2] - 1][1]);
            xs.push_back(nodes[cells[aa][3] - 1][1]);
            std::vector<double> ys;
            ys.push_back(nodes[cells[aa][1] - 1][2]);
            ys.push_back(nodes[cells[aa][2] - 1][2]);
            ys.push_back(nodes[cells[aa][3] - 1][2]);
            cell_centres.push_back(polycentre(xs,ys,aa+1));
        }
        else if(cells[aa].size() == 6)
        {
            std::vector<double> xs;
            xs.push_back(nodes[cells[aa][1] - 1][1]);
            xs.push_back(nodes[cells[aa][2] - 1][1]);
            xs.push_back(nodes[cells[aa][3] - 1][1]);
            xs.push_back(nodes[cells[aa][4] - 1][1]);
            std::vector<double> ys;
            ys.push_back(nodes[cells[aa][1] - 1][2]);
            ys.push_back(nodes[cells[aa][2] - 1][2]);
            ys.push_back(nodes[cells[aa][3] - 1][2]);
            ys.push_back(nodes[cells[aa][4] - 1][2]);
            cell_centres.push_back(polycentre(xs,ys,aa+1));
        }
    }

    PrintValues("Cell Centres", cell_centres, outfil);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
std::vector GetValues\u n(const std::vector&src,int start,int end)
{
std::载体ret;

对于(int i=start;i这里最明显的事情是在
push_back
调用之前保留存储。这样,向量的大小只调整一次:

std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
    std::vector<double> ret;
    ret.reserve( end - start + 1 );

    for(int i = start; i <= end; ++i)
    {
        ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    return ret;
}
std::vector GetValues\n(const std::vector&src,int start,int end)
{
std::载体ret;
反向备用(结束-开始+1);

对于(inti=start;i除了@Paddy所说的,还要记住:当你做向量时,复制向量是涉及到的。 您正在使用“c_str()”。请调查它是否足够有效

还有一件事:GetValues\u n和GetValues\u c是按值返回向量的。您通过引用获取了向量参数。这是一件好事,从效率角度看。
你也可以尝试引用向量吗?< /p>你是否已经对C++代码进行了剖析,看看热点在哪里?VisualC++快速表没有分析器,NETBeas没有C++分析器……没有分析,这些问题几乎不可能回答。它可以是你读/解析数字的方式,它可以是你的向量,或者它。可能是别的。如果你在使用VC++,那么你可能在windows上,不是吗?我听说windows上有免费的分析器:只是一个猜测,MTLAB会使用SSE2,3,4…SSE可以计算4(使用SSE4的8倍)一个好的GPU可以使用1500个单位进行计算,这意味着它们可以同时进行1500个加法、mul、div等运算…@AlexByasse是一个简单的分析器,可以与VC++Express一起使用。还有一种经过验证的方法,就是随机暂停程序。大多数程序m处于“慢”阶段,因此大多数时候您都会暂停使用一个。双精度复制构造函数非常便宜。如果使用新的向量,编译器应该自动移动返回的向量。否则,调用
swap
可以获得同样的好处。“您也可以尝试通过引用返回向量吗?”!!!永不!!!返回对该函数堆栈上的对象的引用!