From another question here there seems to be evidence, that GCC's implementation of std::remove_if
doesn't provide equally efficiency compared to the following implementation:
'raw homebrew' solution:
static char str1[100] = "str,, ing";
size_t size = sizeof(str1)/sizeof(str1[0]);
int bad = 0;
int cur = 0;
while (str1[cur] != '') {
if (bad < cur && !ispunct(str1[cur]) && !isspace(str1[cur])) {
str1[bad] = str1[cur];
}
if (ispunct(str1[cur]) || isspace(str1[cur])) {
cur++;
} else {
cur++;
bad++;
}
}
str1[bad] = '';
Timing outputs:
0.106860
Sample benchmarking code for std::remove_if
for a solution of the same problem:
bool is_char_category_in_question(const char& c) {
return std::ispunct(c) || std::isspace(c);
}
std::remove_if(&str1[0], &str1[size-1], is_char_category_in_question);
Timing outputs:
1.986838
Check and get actual runtime results for the code running the ideone links above please (giving the full codes here would obscure the question!).
Given the provided execution time results (from the samples), these seem to confirm the first implementation is having much better performance.
Can anyone tell reasons, why the std::remove_if()
algorithm doesn't (or can't) provide a similarly efficient solution for the given problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…