473,503 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which one is better

Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?

Sep 1 '06 #1
10 2440
DaVinci schrieb:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
In which respect?
Tell us what _you_ think.
If this is a homework question, consider just admitting it.

Note that the former may break because you forgot to put x and
y in parentheses and, even if you had not, because x and y are
replaced twice by macro expansion which breaks for x and y that
have side effects.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 1 '06 #2
"DaVinci" <ap***********@gmail.comwrote:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
Yes. No. Because. Depends.

What do you _want_ from this macro? That's the important thing.

Richard
Sep 1 '06 #3
DaVinci posted:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?

The first macro checks whether the first characters are the same. If they
aren't, then it returns. If they are, then it invokes strcmp to check
whether the entire strings are equal. It's taking advantage of "short-
circuit evaluation". 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.

Which one is more efficient will be dependant upon:

(1) The efficiency of the strcmp algorithm
(2) The amount of strings tested which are actually equal.
(3) The amount of strings tested which have the same initial character.
(4) Other stuff specific to the implementation.

If you want to test it on your own system, then run each of them in a loop
several thousand times and time them.

--

Frederick Gotham
Sep 1 '06 #4
Frederick Gotham <fg*******@SPAM.comwrites:
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.
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Sep 1 '06 #5

"DaVinci" <ap***********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
It seems silly to even consider such a macro.

In your code, to use the macro, you would have to write:
if ( STREQL( str1, str2 ) ) {
...
}

when you could dispense with the macro altogether and just write
if ( strcmp( str1, str2 ) == 0 ) {
...
}

or even
if ( !strcmp( str1, str2 ) ) {
...
}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 1 '06 #6
DaVinci wrote:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
I should think that there isn't a difference. Just
call strcmp(x,y). I'd expect strcmp to return
immediately if the first characters of the string
differs anyway.

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:-)

--
goose
Have I offended you? Send flames to root@localhost
real email: lelanthran at gmail dot com
website : www.lelanthran.com
Sep 2 '06 #7
DaVinci wrote:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?
This will depend largely on the scenario.

In the first macro, there is reference made to x and y twice. This
means that there are cases where the two macros do not behave
equivalently (consider the case when x is ++p, for example).

In the majority of cases they should behave equivalently, so there is
just the question of speed. The first macro may seem to save you a
function call, which is good if the strings are typically not equal in
the first character. However, the compiler may inline and perform this
optimization (or the equivalent) for you when using the second macro
automatically anyways. You might go ahead and time it on a few
scenarios -- I think it may vary a lot depending on both the compiler
and scenario.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Sep 2 '06 #8
On Fri, 01 Sep 2006 09:45:13 -0700, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
>Frederick Gotham <fg*******@SPAM.comwrites:
>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.

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.
Ben gives good advice.

In my experience, over-optimization can be the root of all evil.

Although the following link uses Windows-specific WIN32 function calls
to illustrate the author's point, it nevertheless illustrates concepts
that apply in a broad general sense.

http://www.flounder.com/optimization.htm

Best regards
--
jay

Sep 2 '06 #9

In article <ed**********@ctb-nnrp2.saix.net>, goose <lk************@webmail.co.zawrites:
DaVinci wrote:
Is #define STREQL(x,y) ((*x == *y)&&(strcmp((x),(y))==0)) is bettern
than STREQL(x,y) (strcmp((x),(y))==0).why?

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:-)
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)))

which would be a net win if you very frequently compare two strings
that are actually the same object, and function calls are really
expensive in your implementation, and you never use the macro with
parameters that have side effects, and you compare strings in the
inner loop, and your program is very performance sensitive, and...

Oh, from the fan the feces flies,
When we first try to optimize.

(And they say the US has no tradition of light verse.)

--
Michael Wojcik mi************@microfocus.com

Pocket #16: A Ventriloquist's "Helper" -- Recordings for Divers Occasions,
especially cries to put in the mouths of enemies -- "God Bless Captain
Vere!" "Les jeux sont faits!" &c. -- Joe Green
Sep 3 '06 #10
Michael Wojcik wrote:
>
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)))
Just as a by-the-way, I once encountered a strcmp() that
performed this optimization internally, returning zero almost
immediately if its two pointer arguments were equal. I suspect
(but did not verify) that this wasn't because of an explicit
test for equality, but something that "fell out" of other tests
to determine whether the strings' alignments permitted comparing
them in four- or eight-byte chunks rather than byte-by-byte.

--
Eric Sosman
es*****@acm-dot-org.invalid
Sep 3 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3321
by: Amit D.Shinde | last post by:
Hello Experts.. I need some help regarding cookies and session objects and also global.asa file I am creating one cookie when a user logs in on my website. The cookie stores the login name of...
6
2126
by: lastusernameleft | last post by:
i've been researching this issue for a while and can't come to any conclusive answer, mostly it seems to be a preference over syntax, some saying c# is elegant while vb is clunky, or that c# is...
1
3464
by: Markus Rebbert | last post by:
Hi list, i got an postgresql installation on linux (debian) with the htree partitions: 1- system 2- postgresql data files 3- postgresql WAL logs(pg_xferlog) Our standard file system is...
4
2923
by: Ed Davis | last post by:
I'm trying to decide which of the following programming styles is better, as in easier to understand, and thus easier to maintain. Keep in mind that for posting purposes, this is a greatly...
9
288
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment.
22
2193
by: smartwolf agassi via DotNetMonster.com | last post by:
I'm a C# language learner. I want to know which IDE is better for C# programing, Borland C#Builder or VS.net 2003? -- Message posted via http://www.dotnetmonster.com
2
2880
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
2522
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
48
4881
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
20
3042
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.