c++ vs MATLAB 速度优化。例如。我的 matlab 代码运行速度比 C++ 快?

标签 c++ performance matlab optimization

我编写了一个程序,该程序读取带有单元格顶点坐标的 ascii 文件,然后输出带有单元格中心的文件。有什么办法可以加快 c++ 代码的速度,至少与我的 matlab 性能相匹配。我在我的 C++ 代码中使用 push_back vector ,但我不知道如何使用数组实现类似的效果??

我正在尝试使用 sleepy 进行分析,但是在设置程序之前如何让 sleepy 开始分析它只能运行已经运行的程序,而且输出非常困惑,不要转到我的代码行但是std::_Vector_cal >.... 等行。 exclusive 和 inclusive 之间有什么区别,最高 % exclusive 名称是 operator delete,其次是 operator new????

我的 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;
}

最佳答案

这里最明显的事情是在 push_back 调用之前保留存储空间。这样 vector 只调整一次大小:

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;
}

关于c++ vs MATLAB 速度优化。例如。我的 matlab 代码运行速度比 C++ 快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19040947/

相关文章:

c++如何在满足某个参数时停止所有功能

c++ - 如何使用 gluLookAt 在 OpenGL 中围绕某个点旋转相机 View

c++ - 为什么我的指针输出一个字符串而不是 C++ 中的内存地址?

c++ - 如何将聚合支持到 STL 或类似 STL 的库中

c# - 降低 C# for 循环的性能以获得比 C for 循环更大的范围

matlab - 在 MATLAB 中处理多个绘图

MATLAB 不会返回我的输出

c++ - 将成员函数的本地存储分配移动到其类是否可以获得性能?

c++ - 在 C++ 中比较 & 与 % 的速度

arrays - 如何在Matlab中找到最接近指定索引的索引