C#文字列がNullまたはnullであると判断する方法
4745 ワード
文字列がNullまたはnullであると判断する方法
テスト結果
テスト1-IsNull OrEmpty:943 ms-p==null‖p=":1093 ms-p==null‖p.Length==0:831 ms-(p+").Length==0:1115 msテスト2-IsNull OrEmpty:850 ms-p==null‖p=":1005 ms-p==null‖p.Length==0:847 ms-(p+").Length==0:1077 ms
結論
Code
var sw = new System.Diagnostics.Stopwatch();
var p = default(string);
var q = "";
var r = "str";
sw.Reset();
sw.Start();
for (var i = 1; i < 100000000; i++)
{
var pR = string.IsNullOrEmpty(p);
var qR = string.IsNullOrEmpty(q);
var rR = string.IsNullOrEmpty(r);
}
sw.Stop();
Console.WriteLine("IsNullOrEmpty: " + sw.ElapsedMilliseconds.ToString() + "ms");
sw.Reset();
sw.Start();
for (var total = 1; total < 100000000; total++)
{
var pR = p == null || p == "";
var qR = q == null || q == "";
var rR = r == null || r == "";
}
sw.Stop();
Console.WriteLine("p == null || p == \"\": " + sw.ElapsedMilliseconds.ToString() + "ms");
sw.Reset();
sw.Start();
for (var total = 1; total < 100000000; total++)
{
var pR = p == null || p.Length == 0;
var qR = q == null || q.Length == 0;
var rR = r == null || r.Length == 0;
}
sw.Stop();
Console.WriteLine("p == null || p.Length == 0: " + sw.ElapsedMilliseconds.ToString() + "ms");
sw.Reset();
sw.Start();
for (var total = 1; total < 100000000; total++)
{
var pR = (p +"").Length == 0;
var qR = (q +"").Length == 0;
var rR = (r +"").Length == 0;
}
sw.Stop();
Console.WriteLine("(p +\"\").Length == 0: " + sw.ElapsedMilliseconds.ToString() + "ms");