Hi, can someone please tell me why the second method is faster than the first one?
the second method was about 1.5 times faster than the first one to execute.
So I’m just curious what makes the first one slower than the second.
void ofApp::getBoundingBoxDimen1(float &boxWidth, float &boxHeight, const vector<ofVec3f> &vertices) {
auto xExtremes = minmax_element(vertices.begin(), vertices.end(),
[](const ofVec3f& lhs, const ofVec3f& rhs) {
return lhs.x < rhs.x;
});
auto yExtremes = minmax_element(vertices.begin(), vertices.end(),
[](const ofVec3f& lhs, const ofVec3f& rhs) {
return lhs.y < rhs.y;
});
boxWidth = xExtremes.second->x - xExtremes.first->x;
boxHeight = yExtremes.second->y - yExtremes.first->y;
}
void ofApp::getBoundingBoxDimen2(float &boxWidth, float &boxHeight, const vector<ofVec3f> &vertices) {
ofRectangle box;
for(size_t i=0; i<vertices.size(); ++i) {
if(i == 0) {
box.set(vertices[i],0,0);
} else {
box.growToInclude(vertices[i]);
}
}
boxWidth = box.width;
boxHeight = box.height;
}