Answer the question
In order to leave comments, you need to log in
And clang again: Why (during optimization) the implementation on pointers turned out to be worse than using the [ ] operator?
In general, the essence is this, there is a code:
int str_cmp1(const char *s, const char *t)
{
while (*s == *t && *s != '\0')
++s, ++t;
return *s - *t;
}
int str_cmp2(const char *s, const char *t)
{
int i;
for (i = 0; s[i] == t[i] && s[i] != '\0'; ++i)
;
return s[i] - t[i];
}
Answer the question
In order to leave comments, you need to log in
Another thing is interesting here. The compiler turns the first version into the second - base-index addressing is very fast, and adding one twice is not the best option. In addition, it detects parallel loops and arranges the same index for them.
By the way, look at the interest for the sake of gcc -Os.
In the first case, the first iteration of the loop is executed outside the loop (hence the larger size). In the second variant, all iterations are performed in a loop.
The code inside the loop is the same. The execution speed will be the same.
The only difference is that the first option is a few bytes larger.
By the way, gcc shows identical code in both cases.
I offer another option:
int str_cmp1(const char *s, const char *t)
{
while (*s == *t++ && *s != '\0') ++s;
return *s - *t;
}
The compiler cannot understand that pointer variables are no longer used anywhere (for example, that one of them does not point to the second) and maintains their current value, which interferes with optimization.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question