From ea2fc4cc510e8324be87634edf3e9c25f787212f Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Sat, 4 Nov 2017 17:20:51 -0700 Subject: [PATCH] Use stable_sort in lod_rank_table (#5378) It is easy to debug and test when use `stable_sort`and the time complexity is not changed. --- paddle/framework/lod_rank_table.cc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/paddle/framework/lod_rank_table.cc b/paddle/framework/lod_rank_table.cc index f9abf902a1..68a83def7e 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 -- GitLab