c++ 与 MATLAB 针对速度的优化.例如,我的 matlab 代码运行速度比 c++ 快
c++ vs MATLAB optimization for speed. E.g. my matlab code runs faster than c++?
我编写了一个程序,该程序读取具有细胞顶点坐标的ascii文件,然后输出具有细胞中心的文件。有什么方法可以加快 c++ 代码的速度,至少可以匹配我的 matlab 性能。我在 c++ 代码中使用push_back向量,但我不知道如何用数组实现类似的效果??
我试图使用 sleepy 进行分析,但是在设置程序之前,您如何开始分析,它只能对已经在运行的运行进行编程,而且输出非常混乱,不要转到我的代码行,而是像 std::_Vector_cal> 这样的行......等等等等。独占和包含有什么区别,最高的%独占名称是运算符删除,其次是运算符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
调用之前保留存储。 这样,矢量只会调整一次大小:
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;
}
除了@Paddy所说的之外,还要记住这一点:当你做一个vector.push_back时,会涉及复制cTor。您正在使用"c_str()"。请调查它是否足够有效。
还有一件事:GetValues_n 和 GetValues_c 是按值返回向量。您已经通过引用获取了向量参数。这是效率方面的好事。您也可以尝试通过引用返回向量吗?
相关文章:
- 为什么在读取文件大小时文件IO速度会发生变化
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 文件系统:复制功能的速度秘诀是什么
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 在C++中使用并行化的预期速度是多少(不是 OpenMp,而是 <thread>)
- 两个连续的 OpenMP 并行区域会相互减慢速度
- 查找标准::hash_map与标准::矢量的速度
- 加快在C++中读取/处理日志文件的速度
- 为什么这些算法的运行速度比它们应该的要快?
- 如何提高文件的读取速度?
- 通过libpqxx提高PostgreSQL数据库的更新速度
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- 计算车辆之间的距离并设置速度,使距离保持不变,例如 5 米
- 如何加快大字符串的解析速度?
- 如何比较两个函数的速度和性能
- 线程相互减慢速度
- 多线程减慢程序速度:无错误共享,无互斥锁,无缓存未命中,无小工作量
- 使用 Unity 构建加快C++构建速度,并减少标头依赖项
- 从 OpenCV 3 切换到 OpenCV 4 会导致网络摄像头以最大 5 fps 的速度录制,而不是通常的 30 f
- 如何构建模板的显式实例化以提高编译速度?