473,657 Members | 2,571 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 2455
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,*S TREQL_str2;

#define STREQL(x,y) \
(STREQL_str1 = (x),STREQL_str2 = (y), \
*STREQL_str1 == *STREQL_str2 \
&& !strcmp(STREQL_ str1,STREQL_str 1))

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,*S TREQL_str2;

#define STREQL(x,y) \
(STREQL_str1 = (x),STREQL_str2 = (y), \
*STREQL_str1 == *STREQL_str2 \
&& !strcmp(STREQL_ str1,STREQL_str 1))

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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Sep 1 '06 #5

"DaVinci" <ap***********@ gmail.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.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.stanfor d.edu>
wrote:
>Frederick Gotham <fg*******@SPAM .comwrites:
>To avoid multiple evaluation, it could be rewritten as:

#include <string.h>

char const *STREQL_str1,*S TREQL_str2;

#define STREQL(x,y) \
(STREQL_str1 = (x),STREQL_str2 = (y), \
*STREQL_str1 == *STREQL_str2 \
&& !strcmp(STREQL_ str1,STREQL_str 1))

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**********@c tb-nnrp2.saix.net> , goose <lk************ @webmail.co.zaw rites:
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

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

Similar topics

2
3336
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 the user. I want that cookie should get deleted when user closes the browser without signing out. I think it is done in global.asa file . But i don;t know how to do it?
6
2151
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 geeky while vb is more naturally legible. there dont seem to be many capabilities in one over the other, at least with whidbey, so basically, is there anything c# can do that vb can't?
1
3474
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 ext3. Now i heard, xfs is better for the data files.
4
2936
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 simplified example. The goal is to parse and build an AST for a print statement, returning the base-node of the AST built. print ::= "print" expr | string ("," expr | string )*
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
2222
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
2892
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
2571
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
4919
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 David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
20
3065
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 like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1607
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.