判断字符串变量str是否为空的一下三种方法哪个性能更优 判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?据说答案是C,不太明白,请问为什么啊?谢谢~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我认为是 b 好点 ,String.Empty String类自带的 表示为否空字符串 a 和 b 是等同的,因为 String.Empty 就是 ""c 最快,因为它不用做 string.Equal(); 相当于直接取一个常量值看 string 类的源码就知道了 string.IsNullOrEmpty(str)最优秀,但不一定是最快! 上面说得好像还是不很清楚,str.Length==0 也进行了判断啊,感觉还是先取出长度再进行的判断,是不是更要浪费时间? 我也很好奇这个问题。感觉str.Length效率总应该要低一些。因为要计算长度嘛,不过应该不是这样。洗耳恭听下面的高手解释 怎么不先判断是否为null呢?:P 应该将a和b,或a和c结合起来用,str==""与str==null是不同的 题目的要求判断str是否为空.当str对象未分配内存,既str=null时,这个字符串也算为空.str==""和str=String.Empty都不能检测str=null的情况 string.IsNullOrEmpty(str)最优秀,但不一定是最快!str.Length==0;虽然快,但是str为null的时候会抛异常的 string.IsNullOrEmpty(str)VS 2005的代码运行推荐用这个,不信你可以试一下。 判断字符串变量str是否为空的一下三种方法哪个性能更优:a、str=="";b、str==String.Empty;c、str.Length==0;?——————————————————————————————————————-----------------------------------------------------string tmp;tmp.Length=0;无法对属性或索引器“string.Length”赋值 -- 它是只读的-----------------------------------------------------推荐使用string.Empty。个人觉得更加更加能够让人理解 tmp.Length 也容易理解= = str.Length==0效率更快1.使用Length来检查一个字符串是否为空。计算Length时,只需要检查内部数组的长度,不需要字节比较。而字节比较是比直接计算计算要慢很多的2.在来看看string的一个构造函数String (Char[], Int32, Int32) 将 String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。即string对象在本身构造时,就已经有一个参数是长度指示,他对外的表现就是Length3.我们在来看看String.Empty本质,有一定c++和vb基础的都知道,一个字符串都有一个表示结尾的字节,net环境下也是如此,这个结尾的字节就是String.Empty4.使用str==String.Empty要比str==""快速因为String.Empty在内部是个常量综上:a、str=="";b、str==String.Empty;c、str.Length==0;c优于b,b优于a str.Length==0因为他是str的属性,一般属性都是初使化时就有,所以你 用他是直接就有的,不用做任何计算。。如果是方法就不同了哈哈 using System;public class strLengthTime{ public static void Main() { string strTest = ""; string strSub; for (int i = 0; i < 1000; i++) { strSub = i.ToString(); strTest = strTest + strSub; } DateTime startTime; DateTime endTime; TimeSpan timeCost; startTime = DateTime.Now; for (int m = 0; m < 100000000; m++) { if (strTest.Length == 0) { } } endTime = DateTime.Now; timeCost = endTime - startTime; System.Console.WriteLine("strTest.Length Method Costs:" + timeCost.TotalMilliseconds.ToString()); startTime = DateTime.Now; for (int m = 0; m < 100000000; m++) { if (strTest == string.Empty) { } } endTime = DateTime.Now; timeCost = endTime - startTime; System.Console.WriteLine("String.Empty Method Costs:" + timeCost.TotalMilliseconds.ToString()); startTime = DateTime.Now; for (int m = 0; m < 100000000; m++) { if (strTest == "") { } } endTime = DateTime.Now; timeCost = endTime - startTime; System.Console.WriteLine("The Third Method Costs:" + timeCost.TotalMilliseconds.ToString()); Console.ReadLine(); }}事实胜于雄辩 to:xiaotupansy(中)用stopwatch测时间间隔会准确一些:) 学习------------------------http://fenglin.xland.cn------------------------ 上CSDN每天都可以学到很多东西啊。记下了。 string.IsNullOrEmpty();这个方法是标准方法,实际上也是上面有些人用的方法。这是.NET自已封的方法。我用工具反译了一下,这就是它的代码原型:public static bool IsNullOrEmpty(string value){ if (value != null) { return (value.Length == 0); } return true;}基本上和大家的做法是一样的,所以说还是建议使用系统自带的,这样也省事,图个方便嘛,对不对? 1. C# 和 C++ 不一样:不是采用0结尾的。2. 由于原因1,调用Length属性(get_Length方法)时,应该不会通过遍历每个字符串直至尾部来取得。而估计是读取String的private字段:m_stringLength。3. CLR对于字符串常量(注意:常量)采用字符串驻留机制(Microsoft.NET.框架程序设计 12.2.4节):即用一个散列表保存字符串常量。比如代码:string s1 = "hello world";//在散列表中查找“hello world”,结果没找到,就把“hello world”填到了散列表中,并返回引用给s1string s2 = "hello world";//找到了“hello world”,返回它的引用给s2所以,上面的s1和s2指向同一个字符串而非两个4. 基于原因3, String类在判断两个相等时,先判断两个字符串是否指向同一个,即这是两个引用(Int32 变量)比较,见下面String类的代码:(op_Equality会调用下面的)public static bool Equals(string a, string b){ if (a == b) { return true; } if ((a != null) && (b != null)) { return string.EqualsHelper(a, b); } return false;}可见,string s1 = ""; string s2 = "hello world";bool b1 = s1 == "";//语句3bool b2 = s2 == "";//语句4同样使用 == "";方式语句3的速度也比语句4快。5.下面我写了一个StringEmply类,代码如下:public class StringEmpty { public static bool A(string str) { return str == ""; } public static bool B(string str) { return str == string.Empty; } public static bool C(string str) { return str.Length == 0; } }用Dasm反汇编得到下列代码.method public hidebysig static bool A(string str) cil managed{ // 代码大小 12 (0xc) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldstr "" IL_0006: call bool [mscorlib]System.String::op_Equality(string, string) IL_000b: ret} // end of method StringEmpty::A.method public hidebysig static bool B(string str) cil managed{ // 代码大小 12 (0xc) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldsfld string [mscorlib]System.String::Empty IL_0006: call bool [mscorlib]System.String::op_Equality(string, string) IL_000b: ret} // end of method StringEmpty::B.method public hidebysig static bool C(string str) cil managed{ // 代码大小 10 (0xa) .maxstack 8 IL_0000: ldarg.0 IL_0001: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0006: ldc.i4.0 IL_0007: ceq IL_0009: ret} // end of method StringEmpty::CB 较 A 多走一步: string [mscorlib]System.String::Empty的结果一定是""综上,个人认为:C > A > B我对查看内部代码时,还存在一些不能确定的问题。所以请各位仁兄指正:对在那里;错在哪里。以满足大伙的好奇心。谢!声明:允许骂娘。 我靠强了解了。str.Length==0;? IE中的打印为什么与实际的不一样啊,帮我看看! 找不到请求的 .Net Framework 数据提供程序。可能没有安装.什么问题啊 web控件 TreeView 中如何取得所选节点的父节点? 導出excel 的問題 关于MSN Space的问题... vs2008解决方案移动位置后不能用了。 存储过程中如何使用like? 请问一个统计读取的问题? 根据月份来判断是本年度还是下年度? 怎么让DropDownList选择改变时去招待一个Javascript. C#.net生成发布后,页面文件出现乱码�Ż���վ 菜鸟学.NET 在GridView中插入新记录的问题
最优秀,但不一定是最快!
当str对象未分配内存,既str=null时,这个字符串也算为空.str==""和str=String.Empty都不能检测str=null的情况
最优秀,但不一定是最快!str.Length==0;
虽然快,但是str为null的时候会抛异常的
——————————————————————————————————————-----------------------------------------------------
string tmp;
tmp.Length=0;
无法对属性或索引器“string.Length”赋值 -- 它是只读的
-----------------------------------------------------推荐使用string.Empty。个人觉得更加更加能够让人理解
效率更快
1.使用Length来检查一个字符串是否为空。
计算Length时,只需要检查内部数组的长度,不需要字节比较。而字节比较是比直接计算计算要慢很多的
2.在来看看string的一个构造函数
String (Char[], Int32, Int32)
将 String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。
即string对象在本身构造时,就已经有一个参数是长度指示,他对外的表现就是Length
3.我们在来看看String.Empty本质,有一定c++和vb基础的都知道,一个字符串都有一个表示结尾的字节,net环境下也是如此,这个结尾的字节就是String.Empty
4.使用str==String.Empty要比str==""快速因为String.Empty在内部是个常量综上:
a、str=="";
b、str==String.Empty;
c、str.Length==0;
c优于b,b优于a
因为他是str的属性,一般属性都是初使化时就有,所以你 用他是直接就有的,
不用做任何计算。。如果是方法就不同了哈哈
public class strLengthTime
{
public static void Main()
{
string strTest = "";
string strSub;
for (int i = 0; i < 1000; i++)
{
strSub = i.ToString();
strTest = strTest + strSub;
}
DateTime startTime;
DateTime endTime;
TimeSpan timeCost;
startTime = DateTime.Now;
for (int m = 0; m < 100000000; m++)
{
if (strTest.Length == 0)
{
}
} endTime = DateTime.Now;
timeCost = endTime - startTime;
System.Console.WriteLine("strTest.Length Method Costs:" + timeCost.TotalMilliseconds.ToString());
startTime = DateTime.Now;
for (int m = 0; m < 100000000; m++)
{
if (strTest == string.Empty)
{
}
}
endTime = DateTime.Now;
timeCost = endTime - startTime;
System.Console.WriteLine("String.Empty Method Costs:" + timeCost.TotalMilliseconds.ToString());
startTime = DateTime.Now;
for (int m = 0; m < 100000000; m++)
{
if (strTest == "")
{
}
}
endTime = DateTime.Now;
timeCost = endTime - startTime;
System.Console.WriteLine("The Third Method Costs:" + timeCost.TotalMilliseconds.ToString());
Console.ReadLine();
}
}
事实胜于雄辩
用stopwatch测时间间隔会准确一些:)
http://fenglin.xland.cn
------------------------
这个方法是标准方法,实际上也是上面有些人用的方法。这是.NET自已封的方法。我用工具反译了一下,这就是它的代码原型:public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}
基本上和大家的做法是一样的,所以说还是建议使用系统自带的,这样也省事,图个方便嘛,对不对?
比如代码:
string s1 = "hello world";//在散列表中查找“hello world”,结果没找到,就把“hello world”填到了散列表中,并返回引用给s1
string s2 = "hello world";//找到了“hello world”,返回它的引用给s2
所以,上面的s1和s2指向同一个字符串而非两个4. 基于原因3, String类在判断两个相等时,先判断两个字符串是否指向同一个,即这是两个引用(Int32 变量)比较,见下面String类的代码:(op_Equality会调用下面的)
public static bool Equals(string a, string b)
{
if (a == b)
{
return true;
}
if ((a != null) && (b != null))
{
return string.EqualsHelper(a, b);
}
return false;
}
可见,
string s1 = "";
string s2 = "hello world";
bool b1 = s1 == "";//语句3
bool b2 = s2 == "";//语句4
同样使用 == "";方式语句3的速度也比语句4快。5.下面我写了一个StringEmply类,代码如下:
public class StringEmpty { public static bool A(string str) { return str == ""; }
public static bool B(string str) { return str == string.Empty; }
public static bool C(string str) { return str.Length == 0; } }
用Dasm反汇编得到下列代码
.method public hidebysig static bool A(string str) cil managed{ // 代码大小 12 (0xc) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldstr "" IL_0006: call bool [mscorlib]System.String::op_Equality(string, string) IL_000b: ret} // end of method StringEmpty::A
.method public hidebysig static bool B(string str) cil managed{ // 代码大小 12 (0xc) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldsfld string [mscorlib]System.String::Empty IL_0006: call bool [mscorlib]System.String::op_Equality(string, string) IL_000b: ret} // end of method StringEmpty::B
.method public hidebysig static bool C(string str) cil managed{ // 代码大小 10 (0xa) .maxstack 8 IL_0000: ldarg.0 IL_0001: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0006: ldc.i4.0 IL_0007: ceq IL_0009: ret} // end of method StringEmpty::C
B 较 A 多走一步: string [mscorlib]System.String::Empty的结果一定是""
综上,个人认为:
C > A > B
我对查看内部代码时,还存在一些不能确定的问题。所以请各位仁兄指正:对在那里;错在哪里。以满足大伙的好奇心。谢!
声明:允许骂娘。