Bug 89121 - std::min_element (and max_element) 3.6 times slower than hand written loop
Summary: std::min_element (and max_element) 3.6 times slower than hand written loop
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-30 17:19 UTC by Antony Polukhin
Modified: 2019-01-30 22:56 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Antony Polukhin 2019-01-30 17:19:50 UTC
std::min_element is slow when there's a lot of data and it does not fit into the CPU cache: http://quick-bench.com/tlgxCx9CUMZgOfYbwhFaEI0WNOg
Comment 1 Jakub Jelinek 2019-01-30 17:55:57 UTC
I guess the important difference is that std::min_element doesn't return the minimum value, but iterator for the minimum value.  Finding the element with minimum value is more expensive than the value itself.
Comment 2 Marc Glisse 2019-01-30 18:32:47 UTC
If you write:

  auto mins = *std::min_element(f.begin(), f.end());

so that you ask for the value, the benchmark now says the perf is exactly the same for the 2 versions. I would close this as invalid.
Comment 3 Jakub Jelinek 2019-01-30 22:56:09 UTC
.