jeudi 30 avril 2020

Time difference when sorting vector of structs

I'm trying to sort vectors of 2 structs of different number of element:

    struct1 {
      int id1;
      int id2;
      string name;
      double ts1;
      double ts2;
    }

    struct2 {
      int id1;
      int id2;
      //string name; <-- this was left out
      double ts1;
      double ts2;
    }
std::vector<struct1> vec1;
std::vector<struct2> vec2;

When I tried to sort vec1 and vec2 based on ts1, there is a big difference in the sorting time. The sizes of vec1 and vec2 are large (>100k elements). Does the size of struct affect the sorting?

EDIT: my sorting function

inline bool sorting(const Type &lhs, const Type &rhs) {
    if (lhs.ts1 < rhs.ts2) {return true;}
    else {return false; }
}

std::sort(vec.begin(),vec.end(),
          [this] (Type lhs, Type rhs) { return sorting(lhs,rhs); });

Aucun commentaire:

Enregistrer un commentaire