测试三种常用的字符串判空串方法所耗的时间,出什么问题了?

liucom 2006-12-20 11:08:43
using System;
using System.Threading;
public class strLengthTime
{
public static void Main()
{
string strTest="";
string strSub;
for(int i =0 ;i< 1000;i++)
{
strSub=i.ToString();
strTest = strTest + strSub;
}
//System.Console.WriteLine(strTest);
DateTime startTime;
DateTime endTime;
TimeSpan timeCost;
startTime =DateTime.Now;
if(strTest.Length==0)
{
System.Console.WriteLine("Not A Null String!");
}
else
{
endTime=DateTime.Now;
timeCost=endTime - startTime;
System.Console.WriteLine("strTest.Length Method Costs:"+timeCost.ToString());
}
Thread.Sleep(1000);
startTime = DateTime.Now;
if(strTest==String.Empty)
{
System.Console.WriteLine("Not A Null String!");
}
else
{
endTime=DateTime.Now;
timeCost= endTime - startTime;
System.Console.WriteLine("String.Empty Method Costs:"+timeCost.ToString());
}
Thread.Sleep(1000);
startTime = DateTime.Now;
if(strTest=="")
{
System.Console.WriteLine("Not A Null String!");
}
else
{
endTime=DateTime.Now;
timeCost= endTime - startTime;
System.Console.WriteLine("The Third Method Costs:"+timeCost.ToString());
}
}
}

输出的结果都是00:00:00,出什么问题了?
...全文
127 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liucom 2006-12-20
  • 打赏
  • 举报
回复
我的结果分别是:484.375 ;5218.75 ;4828.125
liucom 2006-12-20
  • 打赏
  • 举报
回复
谢谢xiaotupansy(微雨燕双飞),测试结果出来了,用 string.Length==0方法的速度最快。
xiaotupansy 2006-12-20
  • 打赏
  • 举报
回复
大致上是length的效率最高,empty和""差不多,""稍微好一点
xiaotupansy 2006-12-20
  • 打赏
  • 举报
回复
那里都没有问题,只是电脑所用的时间太短,所以无法显示
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();
}
}
liucom 2006-12-20
  • 打赏
  • 举报
回复
在线等,请各位指教程序到底哪里出问题了

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧