slow std::sort, hot to make it without object copy?
-
I have a vector of millions of points that I have to sort based on their distance from the camera, in order to do that i created the following simple function object:
class PointSorter { public: PointSorter() { points = NULL; } // !!! copy constructor called all the time during std::sort !!! PointSorter(const PointSorter& other) { points = other.points; printf("object copied"); } PointSorter(std::vector\* pvec==NULL) { points = pvec; } bool operator()(const unsigned int&a, const unsigned int&b) { return (\*points)\[a\].z() < (\*points)\[b\].z(); } std::vector\* points; };
then later on I do a simple
PointSorter sorter(&mypoints); // std::vector indices; std::sort( indices.begin(), indices.end(), sorter );
this sorts the indices of my points well, the only concern is that I discovered that the object "sorter" is being copied all the time during std::sort, this shurely impact negatively the sorting performance which is already quite poor for millions of points since I have to achieve interactive frame rates for my 3d program. Does anyone know how to pass a custom sorter object (which is not a static function) that is copied once and used all the way throughout the sorting? Regards, Michele
-
I have a vector of millions of points that I have to sort based on their distance from the camera, in order to do that i created the following simple function object:
class PointSorter { public: PointSorter() { points = NULL; } // !!! copy constructor called all the time during std::sort !!! PointSorter(const PointSorter& other) { points = other.points; printf("object copied"); } PointSorter(std::vector\* pvec==NULL) { points = pvec; } bool operator()(const unsigned int&a, const unsigned int&b) { return (\*points)\[a\].z() < (\*points)\[b\].z(); } std::vector\* points; };
then later on I do a simple
PointSorter sorter(&mypoints); // std::vector indices; std::sort( indices.begin(), indices.end(), sorter );
this sorts the indices of my points well, the only concern is that I discovered that the object "sorter" is being copied all the time during std::sort, this shurely impact negatively the sorting performance which is already quite poor for millions of points since I have to achieve interactive frame rates for my 3d program. Does anyone know how to pass a custom sorter object (which is not a static function) that is copied once and used all the way throughout the sorting? Regards, Michele
what about you change the code of std::sort make it use the 3th parameters pass by reference? :-D