Skip to content

Commit 053e5cd

Browse files
committed
Optimize Output layer top-k implementation
Replace `std::sort` with `std::partial_sort` for better performance when only the k highest values are actually required
1 parent 13b6788 commit 053e5cd

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

include/layers/OutputLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ std::pair<std::vector<std::string>, std::vector<ValueType>> top_k_vec(
104104
for (size_t i = 0; i < input.size(); i++) {
105105
sort_buf[i] = std::make_pair(labels[i], input[i]);
106106
}
107-
std::sort(sort_buf.begin(), sort_buf.end(), compare_pair<ValueType>);
107+
std::partial_sort(sort_buf.begin(), sort_buf.begin() + k, sort_buf.end(), compare_pair<ValueType>);
108108
std::vector<std::string> res_labels(k);
109109
std::vector<ValueType> res_input(k);
110110
for (size_t i = 0; i < k; i++) {

0 commit comments

Comments
 (0)