std::remove_if GCC implementation isn’t efficient?

Looks to me as though you’re running remove_if on a range of 100 characters since size is 100, but the “homebrew” runs until you find the nul terminator (which is only 10 characters in).

Dealing with that using the change in your comment below, on GCC with -O2 I still see a difference of about a factor of 2, with remove_if being slower. Changing to:

struct is_char_category_in_question {
    bool operator()(const char& c) const {
        return std::ispunct(c) || std::isspace(c);

gets rid of almost all of this difference, although there may still be a <10% difference. So that looks to me like a quality of implementation issue, whether or not the test gets inlined although I haven’t checked the assembly to confirm.

Since your test harness means that no characters are actually removed after the first pass, I’m not troubled by a 10% difference. I’m a bit surprised, but not enough to really get into it. YMMV 🙂

Leave a Comment