D
DaVinci
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
than STREQL(x,y) (strcmp((x),(y))==0).why?
DaVinci said:Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
DaVinci said:Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
Frederick Gotham said:To avoid multiple evaluation, it could be rewritten as:
#include <string.h>
char const *STREQL_str1,*STREQL_str2;
#define STREQL(x,y) \
(STREQL_str1 = (x),STREQL_str2 = (y), \
*STREQL_str1 == *STREQL_str2 \
&& !strcmp(STREQL_str1,STREQL_str1))
The second macro simply calls strcmp.
It seems silly to even consider such a macro.DaVinci said:Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
DaVinci said:Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
DaVinci said:Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
I wouldn't recommend doing that. It's not reentrant and it uses
global variables. It's likely to cause surprises later. Instead
of doing that, I'd suggest just writing out the strcmp() call
explicitly where you need to compare strings. Then, later, if
profiling shows that one of those strcmp() calls is a hot spot,
add the short-circuit test in-line where it is needed.
In fact, now that I think about it, the first one
actually should be almost insignificantly slower
in most general cases, defeating the obvious
purpose of short-circuiting (and avoiding a call
to strcmp) because the first character of each
string is going to be tested *and* a call will
be made to strcmp (in the general case, of course
Michael said:Plus, the macro misses out on the further delicious pessimization of
testing for pointer equality:
#define WORSE_STREQL(x,y) ((x) == (y) || \
((*(x) == *(y)) && \
(strcmp((x),(y)) == 0)))
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.