string.Empty与"",null的区别,用哪个更好呢?
来源:互联网 发布:淘宝开什么店最赚钱 编辑:程序博客网 时间:2024/04/28 06:35
null是C#关键字,是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,如果 int i=null,的话,是不可以的,因为Int是值类型的。
string.Empty就相当于"",但他俩和NULL是两码事。
据说是:string.Empty不分配存储空间
""分配一个长度为空的存储空间 所以string.Empty 比""效率要高点。(效率高是对的,但是到底是否分配存储空间,还不明确,待核实)
一个外国人做了一个测试,测试的对象有5种,分别是:
通过下列的测试代码(如要看请点击下面的加号),判断谁的效率更高~
测速代码#region 测速代码
const int cnTries = 10;
const int cnIterations = 1000000000;
DateTime dtStart, dtEnd;
TimeSpan ts;
double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
int i, j;
string sEmpty = string.Empty;
string sShort = “This is a short string to test empty string comparison”;
string sLong = “This is a long string to test the efficiency of comparing with empty strings, which means it has to be like, really long. And I’m starting to run out of useless things to say…”;
for (j = 0; j < cnTries; ++j)
...{
//double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongQuotes += ts.TotalMilliseconds;
//double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongEmpty += ts.TotalMilliseconds;
//double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotQuotes += ts.TotalMilliseconds;
//double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotEmpty += ts.TotalMilliseconds;
//double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotLength += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotLength += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotLength += ts.TotalMilliseconds;
}
Console.WriteLine(“empty: ...{0}”, fEmptyQuotes / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortQuotes / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongQuotes / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyEmpty / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortEmpty / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongEmpty / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotQuotes / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotQuotes / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotQuotes / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotEmpty / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotEmpty / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotEmpty / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotLength / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotLength / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotLength / (double)cnTries);
#endRegion
const int cnTries = 10;
const int cnIterations = 1000000000;
DateTime dtStart, dtEnd;
TimeSpan ts;
double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
int i, j;
string sEmpty = string.Empty;
string sShort = “This is a short string to test empty string comparison”;
string sLong = “This is a long string to test the efficiency of comparing with empty strings, which means it has to be like, really long. And I’m starting to run out of useless things to say…”;
for (j = 0; j < cnTries; ++j)
...{
//double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong == “”) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongQuotes += ts.TotalMilliseconds;
//double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong == string.Empty) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongEmpty += ts.TotalMilliseconds;
//double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotQuotes += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Equals(“”)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotQuotes += ts.TotalMilliseconds;
//double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotEmpty += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Equals(string.Empty)) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotEmpty += ts.TotalMilliseconds;
//double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sEmpty.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fEmptyDotLength += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sShort.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fShortDotLength += ts.TotalMilliseconds;
dtStart = DateTime.Now;
for (i = 0; i < cnIterations; ++i)
...{
if (sLong.Length == 0) ;
}
dtEnd = DateTime.Now;
ts = dtEnd - dtStart;
fLongDotLength += ts.TotalMilliseconds;
}
Console.WriteLine(“empty: ...{0}”, fEmptyQuotes / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortQuotes / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongQuotes / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyEmpty / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortEmpty / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongEmpty / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotQuotes / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotQuotes / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotQuotes / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotEmpty / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotEmpty / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotEmpty / (double)cnTries);
Console.WriteLine(“empty: ...{0}”, fEmptyDotLength / (double)cnTries);
Console.WriteLine(“short: ...{0}”, fShortDotLength / (double)cnTries);
Console.WriteLine(“long : ...{0}”, fLongDotLength / (double)cnTries);
#endRegion
最后得到测试的结果如下:
[s == ""
]
- 空字符串, 10315.6250 毫秒
- 短字符串, 8307.8125 毫秒
- 长字符串, 8564.0625 毫秒
[s == string.Empty
]
- 空字符串, 3573.4375 毫秒
- 短字符串, 8307.8125 毫秒
- 长字符串, 8603.1250 毫秒
[s.Equals("")
]
- 空字符串, 9517.1875 毫秒
- 短字符串, 7537.5000 毫秒
- 长字符串, 7576.5625 毫秒
[s.Equals(string.Empty)
]
- 空字符串, 9540.6250 毫秒
- 短字符串, 7515.6250 毫秒
- 长字符串, 7607.8125 毫秒
[s.Length == 0
]
- 空字符串, 443.7500 毫秒
- 短字符串, 443.7500 毫秒
- 长字符串, 445.3125 毫秒
很明显用字符串的length属性是最快的。
得到以下结论:
用s.Equals("stringtocompare")
来判断非空字符串是否相等,用 s.Length == 0
来判断是否是空字符串(注意这里不能用这个来判断字符串为NULL的情况,否则会出现“未将对象引用设置到对象的实例”的错误)。
在2.0中判断字符串是否为空(包含NULL的情况)用String.IsNullOrEmpty(str) ;
- string.Empty与"",null的区别,用哪个更好呢?
- string.Empty与"",null的区别,用哪个更好呢?
- string.Empty与"",null的区别,用哪个更好呢?
- string.Empty 与 null 的区别
- string.Empty与null的区别
- ” ”、string.Empty、null的区别
- NULL与String.Empty的
- "",string.Empty , Null 区别
- string,empty "" null 区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- DBnull null String.Empty的区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- String.Empty,NULL和""的区别
- 星爷电影中有一个超强道具
- acm pku 1007 DNA Sorting
- 开发人员如何培养自己
- 【YAKOV】心中的五大软件公司
- 华尔街日报:西方媒体让中国出现“新爱国主义”
- string.Empty与"",null的区别,用哪个更好呢?
- 心灵传输者--电影之感
- ASCII 字符表
- ASP分页函数
- 介绍敏捷开发的七种主流武器
- X60/T60的SATA硬盘如何安装xp
- delphi与flash通信交互的文本简单实现
- error LNK2001: unresolved external symbol _main解决办法(转)
- CSDN2008英雄大会+水源聚会