lundi 4 février 2019

Should I consider a static copy to be costly?

When I'm traversing a collection, say a vector of Points where Point is some struct that holds x, y, z data or something, when I need to convert these points to another type one point at at a time, should I consider it as costly as a copy?

For example, if I have a vector<Point>, I obviously try to avoid:

vector<OtherPointType> v;
// populate v
vector<OtherPoint> vo(begin(v), end(v));
SomeOperation(vo); // Can only operate on vector<OtherPointType>

But, is that as expensive as doing:

for_each(..., [](Point const& p)
{
   OtherPointType opt(p);
   SomeOperation(opt); // Can only operator on `OtherPointType`
});

?

My instinct says that because the later is a static copy without any allocations, that it shouldn't be expensive, but I figured it's important enough to ask.

Usually I can use something like Eigen::Map in latter case, but even there I'm still statically writing to two int's I think due to Eigen::Stride (is this even a good solution in this case?) But then there are libraries like CGAL that don't have mapping capabilities, and I'm curious of a good approach to working with them.

Note, my work normally deals with a million points per second, so it's important that I try to reduce expensive copies as much as I can.

Aucun commentaire:

Enregistrer un commentaire