惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - Lucky Jack

在C#中展示嵌入的RTF文件 SQL进行排序、分组、统计的10个新技巧 select into 和 insert into select的区别 Convert的妙用 DataGridView中回车键的妙用 如何去除C#Strings中的空格? Format String for XML Value - Lucky Jack 如何改变字体大小呢? 如何改变字体风格? C# String小技巧 如何避免按回车键时的嗡鸣声? - Lucky Jack - 博客园 如何嵌入图片资源? Lookupedit使用小记 如何优雅的编程? 文件监视器( FileSystemWatcher) 类的使用 - Lucky Jack 反射也可以这样? - Lucky Jack - 博客园 浅谈对象的初始化顺序 经典的属性设置! 经典sql
也谈String.IsNullOrEmpty
Lucky Jack · 2008-02-21 · via 博客园 - Lucky Jack

今天在浏览DevTopics的博客时,发现一篇介绍String的随笔,介绍的是判断一个String变量是否为空时,String的一个方法和一个属性之间的比较,给一个 string变量 's', 下面那个表达式更快?

1.  String.IsNullOrEmpty( s ) 
2.  s == null || s.Length == 0

如果你猜第二个,那么你是对的. 它将比String.IsNullOrEmpty方法快15%,但这种也是以百万分之一秒来衡量的!
这里有一个简单的例子来比较2种方式:

using System;

namespace StringNullEmpty
{
    
class Program
    
{
        
static void Main( string[] args )
        
{
            
long loop = 100000000;
            
string s = null;
            
long option = 0;
            
long empties1 = 0;
            
long empties2 = 0;

            DateTime time1 
= DateTime.Now;

            
for (long i = 0; i < loop; i++)
            
{
                option 
= i % 4;
                
switch (option)
                
{
                    
case 0:
                        s 
= null;
                        
break;
                    
case 1:
                        s 
= String.Empty;
                        
break;
                    
case 2:
                        s 
= "H";
                        
break;
                    
case 3:
                        s 
= "HI";
                        
break;
                }

                
if (String.IsNullOrEmpty( s ))
                    empties1
++;
            }


            DateTime time2 
= DateTime.Now;

            
for (long i = 0; i < loop; i++)
            
{
                option 
= i % 4;
                
switch (option)
                
{
                    
case 0:
                        s 
= null;
                        
break;
                    
case 1:
                        s 
= String.Empty;
                        
break;
                    
case 2:
                        s 
= "H";
                        
break;
                    
case 3:
                        s 
= "HI";
                        
break;
                }

                
if (s == null || s.Length == 0)
                    empties2
++;
            }


            DateTime time3 
= DateTime.Now;

            TimeSpan span1 
= time2.Subtract( time1 );
            TimeSpan span2 
= time3.Subtract( time2 );
            Console.WriteLine( 
"(String.IsNullOrEmpty( s )): Time={0} Empties={1}",
                span1, empties1 );
            Console.WriteLine( 
"(s == null || s.Length == 0): Time={0} Empties={1}",
                span2, empties2 );
            Console.ReadLine();
        }

    }

}
下面是结果:
(String.IsNullOrEmpty( s )): Time=00:00:06.8437500 Empties=50000000
(s == null || s.Length == 0): Time=00:00:05.9218750 Empties=50000000