diff --git a/paddle/framework/lod_rank_table.cc b/paddle/framework/lod_rank_table.cc index f9abf902a13f3ebe5ea75c7456b117462cec8da5..68a83def7e5a12fe3261be9e27cbb9bb54e1f8ad 100644 --- a/paddle/framework/lod_rank_table.cc +++ b/paddle/framework/lod_rank_table.cc @@ -33,10 +33,15 @@ void LoDRankTable::Reset(const LoD& lod, size_t level) { item.length = vec[i + 1] - vec[i]; items_.emplace_back(item); } - std::sort(items_.begin(), items_.end(), - [](const TableItem& a, const TableItem& b) { - return a.length > b.length; - }); + // NOTE(yuyang18): + // + // The time complexity of stable_sort is O(N*log(N)) if additional memory is + // available. It is easy to debug and unit test when using `stable_sort` + // instead of `sort`. Also, the items of a rank table will not be too large. + std::stable_sort(items_.begin(), items_.end(), + [](const TableItem& a, const TableItem& b) { + return a.length > b.length; + }); } } // namespace framework