判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?请问原因是什么??二叉数 binary tree 是一个二元组 BT=(D,R)
R表示结点之间关系的有限集合,D表示结点的有限集合。
请问:一个二元组怎么理解呀。
D,R各是数组,但D集合看成X, R集合看成是Y,这个二元组就是一个坐标呀 P(X,Y)这种理解对吗??
这是数组中的内容
R表示结点之间关系的有限集合,D表示结点的有限集合。
请问:一个二元组怎么理解呀。
D,R各是数组,但D集合看成X, R集合看成是Y,这个二元组就是一个坐标呀 P(X,Y)这种理解对吗??
这是数组中的内容
解决方案 »
- C#读取Excel的难题
- 用代码实现web共享时出现RPC服务器不可用的问题
- 讨论,做的TCP通讯程序当一个服务器负荷满足不了时,再加一个服务器程序应如何处理?
- 关于多个Site使用同一个MasterPage的问题
- C#读写已知格式的二进制文件
- 打印问题
- 关于用SendMessage取SysTreeView32中TreeNode内容的问题
- 怎样获得机器的硬件信息呢?比如CPU厂家和频率
- 当客户从银行汇款到我的卡上时,登陆到银行网上查看时只发现我卡上的金额增长了,没有汇款客户的信息,尤其是在较短的时间内有几个客户时更难区别这些客户,现在考虑有两种办法,但都不太理想,请大家看看还有什么办法。我考虑的两种办法如下:
- linq在原始数组上排序
- 在后台取前台Div或Table的innerHTML或innerText问题
- 求直线和平面的交点~被卡住
static void juge()
{
string str = "csdn";
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == "")
{ }
}
st.Stop();
Console.WriteLine("str == '' : "+st.ElapsedMilliseconds);
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == String.Empty)
{ }
}
st.Stop();
Console.WriteLine("str == String.Empty : " + st.ElapsedMilliseconds);
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str.Length == 0)
{ }
}
st.Stop();
Console.WriteLine("str.Length == 0 : " + st.ElapsedMilliseconds);
}
Result(str = "csnd"):
str == '': 1292
String.Empty: 2584
str.Length: 2955
Result : str = ""
str == '': 676
String.Empty: 2306
str.Length: 2696结论:A最快。欢迎指正。
class Test
{
static void Main()
{
string str = "csdn";
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == "")
{ }
}
st.Stop();
Console.WriteLine("str == '' : " + st.ElapsedMilliseconds);
st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == String.Empty)
{ }
}
st.Stop();
Console.WriteLine("str == String.Empty : " + st.ElapsedMilliseconds);
st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str.Length == 0)
{ }
}
st.Stop();
Console.WriteLine("str.Length == 0 : " + st.ElapsedMilliseconds);
Console.ReadKey();
}
}str == '' : 2984
str == String.Empty : 2967
str.Length == 0 : 426
因为整数判断等于最快,没有经过实例化等复杂的过程。具体看这里:http://blog.csdn.net/21aspnet/archive/2007/04/28/1588047.aspx和9楼结果不同
在后两个st.Start()之前先复位 st.Reset();
结果:
str == '': 1289
String.Empty: 1176
str.Length: 365应该是判断Length最快的。
改正!
↑不好意思,以上答案是错误的,判定为空正确最快应该是方法C,如上面几楼所说,判定整型是最快的。关于这个实验这位弟兄你有没有发现每次最后那个值是最大的。其实st.ElapsedMilliseconds是在累加...如果对我说的有所怀疑,你可在相同系统运行环境下调试这段代码三次,只要你改变一下输出的先后顺序,你就会发现str.Length==0的判定时间大概只有另外两种的五分之一,甚至更少。
以上所述本人也做过和你相近的测试,在本机上运行多次后方法A和B所花时间都在1000~1100左右,但是方法C的时间只有210~230。
所有楼主的问题1答案应该为方法C。问题2本人只可以说,具体情况要具体分析,按楼主所说的按字面上理解是对的。
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str==null || str == "")
{ }
}
st.Stop();
message += "\r\nstr == '' : " + st.ElapsedMilliseconds;
st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == String.Empty)
{ }
}
st.Stop();
message += "\r\nstr == String.Empty : " + st.ElapsedMilliseconds;
st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (str == null || str.Length == 0)
{ }
}
st.Stop();
message += "\r\nstr.Length == 0 : " + st.ElapsedMilliseconds;
st = new System.Diagnostics.Stopwatch();
st.Start();
for (int i = 0; i < 100000000; i++)
{
if (String.IsNullOrEmpty (str))
{ }
}
st.Stop();
message += "\r\nString.IsNullOrEmpty: " + st.ElapsedMilliseconds; this.textBox2.Text = message;
debug状态下:
str = ""str == '' : 1343
str == String.Empty : 2178
str.Length == 0 : 719
String.IsNullOrEmpty: 1127str = "adsad"str == '' : 1926
str == String.Empty : 1907
str.Length == 0 : 684
String.IsNullOrEmpty: 1167str = nullstr == '' : 533
str == String.Empty : 1453
str.Length == 0 : 512
String.IsNullOrEmpty: 922判断不为空:str = ""str == '' : 1433
str == String.Empty : 2344
str.Length == 0 : 694
String.IsNullOrEmpty: 892str = "sfsf"
str == '' : 2108
str == String.Empty : 1999
str.Length == 0 : 707
String.IsNullOrEmpty: 845str = nullstr == '' : 507
str == String.Empty : 1527
str.Length == 0 : 489
String.IsNullOrEmpty: 839当字符串特别长时:str == '' : 1998
str == String.Empty : 1989
str.Length == 0 : 701
String.IsNullOrEmpty: 850应该是这种最快吧
if (str != null && str.Length > 0)
or if (str == null || str.Length == 0)
if (!String.IsNullOrEmpty (str)) 比if (String.IsNullOrEmpty (str))
快...
Y也是集合,比如(a@b,C@d)
这个不能看成坐标吧
确实需要改正,不然误导别人。每次st重新Start()之前,要先Reset()一下。你得出的时间后面的是前面的累积。