gpt4 book ai didi

c++ - 避免在共享库之间传递数据的开销

转载 作者:行者123 更新时间:2023-11-28 04:18:12 26 4
gpt4 key购买 nike

1st共享库中,3D坐标数据以Point结构存储:

struct Point {

float x{0}, y{0}, z{0};

};

std::vector<Point> *m_points;

2nd 共享库中,3D 坐标数据存储在 PointContainer 类中

class PointContainer : public QObject
{
Q_OBJECT

// This is a sophisticated class ...

public:
QVector3D m_coord;
}

QVector<PointContainer> *m_points;

要将数据从2nd 共享库传递到1st,我使用了一个循环:

std::vector<Point> data(number_of_points);
// Prepare the data in the structure needed by the 1st shared library
for (size_t i = 0; i < number_of_points; ++i) {
float x = m_points->at(i).m_coord.x();
float y = m_points->at(i).m_coord.y();
float z = m_points->at(i).m_coord.z();
Point point = {};
point.x = x;
point.y = y;
point.z = z;
data[i] = point;
}
// Now pass data to the 1st shared library

number_of_points 可能很大,并且上述循环的计算量可能很大。有没有办法避免上述循环?我尝试在共享库中存储具有相同结构的数据,但这需要对代码进行大修。不确定是否还有其他选择,只是询问。

最佳答案

这段代码会更简洁一点,速度也稍微快一点:

std::vector<Point> data(number_of_points);
// Prepare the data in the structure needed by the 1st shared library
for (size_t i = 0; i < number_of_points; ++i) {
const auto& source = (*m_points)[i].m_coord;
data[i] = {source.x(), source.y(), source.z()};
}
// Now pass data to the 1st shared library

如果您的点数至少数千万,您可以使用 OpenMP 或英特尔 TBB 的 parallel_for 加速此循环。

关于c++ - 避免在共享库之间传递数据的开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56088295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com