Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. slow std::sort, hot to make it without object copy?

slow std::sort, hot to make it without object copy?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsalgorithmsperformancetutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michele Bosi
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • M Michele Bosi

      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

      D Offline
      D Offline
      Dracula Wang
      wrote on last edited by
      #2

      what about you change the code of std::sort make it use the 3th parameters pass by reference? :-D

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups