哪一个更好 [英] which one is better

查看:94
本文介绍了哪一个更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define STREQL(x,y)((* x == * y)&&(strcmp((x),(y))== 0))是否优惠

比STREQL(x,y)(strcmp((x),(y))== 0)。为什么?

解决方案

DaVinci schrieb:


是#define STREQL(x,y)((* x == * y)&&(strcmp((x),( y))== 0))比STREQL(x,y)更优惠

(strcmp((x),(y))== 0)。为什么?



在哪方面?

告诉我们_you_的想法。

如果这是一个家庭作业问题,请考虑只是承认它。


请注意,前者可能会因为忘记将x和
$ b $括在括号中而破坏,即使你没有,因为x和y是

由宏扩展替换两次,这会打破x和y,即

有副作用。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


< BLOCKQUOTE>"达芬奇" < ap *********** @ gmail.comwrote:


#define STREQL(x,y)((* x == * y)&&(strcmp((x),(y))== 0))比STREQL(x,y)更优惠
(strcmp((x),(y) )== 0)。为什么?



是的。没有为什么。取决于。


你从这个宏中得到了什么?这是重要的事情。


Richard


DaVinci发布:


是#define STREQL(x,y)((* x == * y)&&(strcmp((x),(y))== 0))是最好的

比STREQL(x,y)(strcmp((x),(y))== 0)。为什么?



第一个宏检查第一个字符是否相同。如果他们不是b $ b,那么它就会回来。如果是,则调用strcmp检查

是否整个字符串相等。它正在利用短 -

电路评估。为了避免多次评估,可以将其改写为

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

第二个宏只是调用strcmp。


哪一个效率更高将取决于:


(1)效率strcmp算法

(2)测试的字符串数量实际上相等。

(3)测试的字符串数量具有相同的初始字符。

(4)其他特定于实现的东西。


如果你想在自己的系统上测试它,那么在一个循环中运行它们

几千次并计时。


-


Frederick Gotham


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

解决方案

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.


"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


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


这篇关于哪一个更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆