*On 30/04/2015 at 04:59, xxxxxxxx wrote:*

User Information:

Cinema 4D Version: R14+

Platform: Windows ;

Language(s) : C++ ;

---------

Hi guys,

I'm going through some of my existing code and trying to optimise it, make it faster, and generally improve my knowledge of C++ and C4D SDK at the same time.

What i have atm is a function called poly_center, which takes a Polygon and returns the global position of the average center of it..

This is fine and works, but is required to be run hundreds/thousands of times or more, and so i would like to focus on optimising the code for it.

Here is the function itself - any thoughts, suggestions, advice and experience is appreciated:

```
Vector poly_center(PolygonObject* op, CPolygon* polygon)
{
std::vector<long> indices;
indices.push_back(polygon->a);
indices.push_back(polygon->b);
indices.push_back(polygon->c);
//# Determine whether the polygon is a triangle or a quadrangle
if (polygon->c != polygon->d)
{
indices.push_back(polygon->d);
}
//# Gather a list of the points' Vectors
std::vector<Vector> vectors;
for (int i=0; i<int(indices.size()); i++)
{
Vector pnt = op->GetPointR()[indices[i]];
vectors.push_back(pnt);
//vectors = map(lambda i: op->GetPoint(i), indices);
}
//# Compute the average of the Vectors
Vector sum;
for(std::vector<Vector>::iterator j=vectors.begin(); j<vectors.end(); j++)
{
sum += *j;
}
return ( sum / vectors.size() ) *op->GetMg();
}
```