源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

asp.ent(C#)中判断空字符串的3种方法以及性能分析

  • 时间:2020-06-16 21:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:asp.ent(C#)中判断空字符串的3种方法以及性能分析
3种方法分别是: string a=""; 1.if(a=="") 2.if(a==String.Empty) 3.if(a.Length==0) 3种方法都是等效的,那么究竟那一种方法性能最高呢?本人用实验说明问题。 建立3个aspx页面(为什么用网页,主要是利用Microsoft Application Center Test ) WebForm1.aspx
[u]复制代码[/u] 代码如下:
private void Page_Load(object sender, System.EventArgs e) { string a=""; for(int i=0;i<=1000000;i++) { if(a=="") { } } }
WebForm2.aspx
[u]复制代码[/u] 代码如下:
private void Page_Load(object sender, System.EventArgs e) { string a=""; for(int i=0;i<=1000000;i++) { if(a==String.Empty) { } } }
WebForm3.aspx
[u]复制代码[/u] 代码如下:
private void Page_Load(object sender, System.EventArgs e) { string a=""; for(int i=0;i<=1000000;i++) { if(a.Length==0) { } } }
在Microsoft Application Center Test 下建立3个压力测试项目: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100103_0_16796.gif[/img] 测试结果: WebForm1.aspx----------if(a=="") [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100104_1_19917.gif[/img] WebForm2.aspx-------if(a==String.Empty) [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100104_2_46176.gif[/img] [b]WebForm3.aspx-------if(a.Length==0)[/b] [b][img]http://img.1sucai.cn/uploads/article/2018010710/20180107100104_3_87903.gif[/img] 所以3种方法量化的结果是98,105,168: [/b]
方法 结果
if(a=="") 98
if(a==String.Empty) 105
if(a.Length==0) 168
[b]那么为什么if(a.Length==0)最快呢? 因为整数判断等于最快,没有经过实例化等复杂的过程。[/b] [b]所以:建议大家判断字符串是否为空用 if(a.Length==0)。[/b]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部