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

Avoid overhead of passing data between shared libraries

本文关键字:数据 开销 之间 共享      更新时间:2023-10-16

in 1st 共享库,3D坐标的数据与Point结构一起存储:

struct Point {
    float x{0}, y{0}, z{0};
};
std::vector<Point> *m_points;

in 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或Intel TBB的Parallel_for加速此循环。