哪个更快 [英] Which is faster

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

问题描述

大家好,只是检查哪种方法更快


string A;

A ="";


if(A == string.Empty)

{

MessageBox.Show(" a is empty");

}

else

{

MessageBox.Show(" a is not empty");

}

//或者这个更快

if(A.Equals(""))

{

MessageBox.Show(" a is empty");

}

else

{

MessageBox.Show (a不为空);

}

这里的参数是我们使用字符串的第一种方法

属性为空然后是等式==并且在场景2中我们使用

对象方法等于比较(所以在= =我们调用Equals,

有意义,是= =重载Equals()?),因此哪1是

更快...用sum1辩论这个我们将在这里发布

希望我们在正确的轨道上.. 。

chow

Hello all, just checking which method is faster

string A;
A = "";

if (A == string.Empty)
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
//or is this quicker
if (A.Equals(""))
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
The argument here is in the first method we are using the string
property empty and then the equality == and in scenario 2 we are using
the object method equals to compare(so inside == we are calling Equals,
make sense, is == overloading Equals() ?), thus which 1 is
quicker...debating this with sum1 so we tht we''d post it here
hope we on the right track...
chow

推荐答案

hi


大问题是,它有多重要吗?


如果答案是肯定的,那么将代码置于10K迭代的循环内,并且

看哪个更快。

欢呼,

-

Ignacio Machin,

ignacio.machin AT dot。 state.fl.us

佛罗里达州交通局

< ni ***** @ yahoo.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
hi

The big question is, does it matter that much?

If the answer is YES, then put the code inside a loop of 10K iterations and
see which is faster.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ni*****@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
大家好,只是检查哪种方法更快

字符串A;
A ="" ;;

if(A == string。空)
{MessageBox.Show(" a is empty);
}
其他
{MessageBox.Show(" a is)不是空的);
}
//或者这样更快
if(A.Equals(""))
{MessageBox.Show(" ; a是空的;
}
其他
{MessageBox.Show(a不为空);
}
论证这是第一种方法,我们使用字符串
属性为空,然后是等于==,在方案2中我们使用
对象方法等于比较(所以在= =我们调用Equals,
有道理,是= =重载Equals()?),因此哪一个更快......用sum1辩论这个,所以我们在这里发布
希望我们在正确的轨道...... chow
Hello all, just checking which method is faster

string A;
A = "";

if (A == string.Empty)
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
//or is this quicker
if (A.Equals(""))
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
The argument here is in the first method we are using the string
property empty and then the equality == and in scenario 2 we are using
the object method equals to compare(so inside == we are calling Equals,
make sense, is == overloading Equals() ?), thus which 1 is
quicker...debating this with sum1 so we tht we''d post it here
hope we on the right track...
chow



< ni ***** @ yahoo.com>写道:
<ni*****@yahoo.com> wrote:
大家好,只是检查哪种方法更快

字符串A;
A ="" ;;

如果(A == string.Empty)
{MessageBox.Show(" a is empty);
}

{// @ MessageBox。显示(a不为空);
}
//或者这样更快
if(A.Equals("))
{
MessageBox.Show(" a is empty");
}
其他
{MessageBox.Show(" a is not empty");
}
这里的论点是第一种方法,我们使用字符串
属性为空,然后是等于==,在场景2中我们使用
对象方法等于比较(所以里面= =我们正在调用Equals,
有意义,是= =重载Equals()?),因此哪一个更快......用sum1辩论这个,所以我们在这里发布<希望我们走在正确的轨道上......
Hello all, just checking which method is faster

string A;
A = "";

if (A == string.Empty)
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
//or is this quicker
if (A.Equals(""))
{
MessageBox.Show("a is empty");
}
else
{
MessageBox.Show("a is not empty");
}
The argument here is in the first method we are using the string
property empty and then the equality == and in scenario 2 we are using
the object method equals to compare(so inside == we are calling Equals,
make sense, is == overloading Equals() ?), thus which 1 is
quicker...debating this with sum1 so we tht we''d post it here
hope we on the right track...




好吧,他们有不同的语义 - 如果A是

null,第一个不会爆炸,而第二个会抛出异常。


如果你''真的*只是在速度之后并且已经知道A是非

null,我相信使用A.Length == 0比
$ b $更快b上述方法。但是,我强烈建议你不要以这种方式微观优化
优化 - 以你发现的最多的方式编写代码

可读,然后看看整体系统的性能,如果你需要改进它,找到瓶颈,并尝试优化那些

,如果你不得不这样做。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



Well, they have different semantics - the first won''t blow up if A is
null, whereas the second will throw an exception.

If you''re *really* just after speed and already know that A is non-
null, I believe that using A.Length==0 is faster than either of the
methods above. However, I would *strongly* urge you not to micro-
optimise in this way - write the code in whatever way you find most
readable, and then look at the performance of the system overall, and
if you need to improve it, find bottlenecks and try to optimise those
if you have to.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


>大家好,只需检查哪种方法更快


我相信


如果(A.Length == 0)


在当前实现上稍快一些。但除非你要多次运行这段代码,否则很可能无关紧要

我建议你写下你最容易找到的内容阅读。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。
>Hello all, just checking which method is faster

I believe

if ( A.Length == 0 )

is somewhat faster on current implementations. But unless you''re going
to run this code lots and lots of times, it probably doesn''t matter
and I suggest you write what you find easiest to read.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


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

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