避免在共享库之间传递数据的开销
Avoid overhead of passing data between shared libraries
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加速此循环。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 实现无开销push_back的最佳方法是什么
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 虚拟继承的性能开销(如果只有一个基具有数据成员)
- 避免在共享库之间传递数据的开销
- 在消息中包装数据的开销
- 返回对成员变量数据的引用的开销
- C++:如果所有数据可能都已有效,则在构造函数中进行验证是否有开销
- Qt 属性、底层数据成员、绑定和开销
- 用于编辑文本的最小cpu和内存开销数据结构
- 在优化和定时的c++代码中从函数中获取数据所带来的难以解释的开销