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

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 三月三

Python写的宇宙模拟器源代码分享 酷我K歌手机控制 [原创]ASP.NET中JQuery跨域的解决方案(HttpModule) Andoid游戏【真情表白】让你心爱的人在游戏中感受真情! [原创]使 PropertyGrid 控件显示中文属性 让所有人大开眼界的震撼创新:第六感 SVN提交更新的一些准则(转载) 除了截屏,该图片你无法保存,不信试试看! 超酷网页 Message Box 样式集合 不看不知道,Google真奇妙 如何为歌曲编配和弦 防止上网受骗-使用网站黑白名单调查工具 - 三月三 想在月球上安个家吗?-趣味站点:入住月球 比较Cool的Web表单生成器 - 三月三 IP经纬度ACCESS数据库 15款图片批量处理软件下载 - 三月三 超级便宜的国外虚拟主机ucvhost–1美元/月 - 三月三 2008年有趣网站大全 - 三月三 美食大全索引
对于大批量赋值功能,使用if判断是否能提高性能
三月三 · 2013-07-22 · via 博客园 - 三月三

场景:

如果对某变量进行赋值,是否需要判断一下,如果相等就不用赋值,这样会不会提高性能。

代码如下:

 1             string a = "1234567890";
 2             string b = "1234567890";
 3 
 4             long x1=0, x2=0, x3=0;
 5 
 6             Stopwatch w = new Stopwatch();
 7 
 8             for (int x = 0; x < 10; x++)
 9             {
10                 w.Reset();
11                 w.Start();
12                 for (int i = 0; i < 100000000; i++)
13                 {
14                     a = b;
15                 }
16                 w.Stop();
17                 x1 += w.ElapsedMilliseconds;
18                 
19 
20 
21                 w.Reset();
22                 w.Start();
23                 for (int i = 0; i < 100000000; i++)
24                 {
25                     if(a != b)
26                         a = b;
27                 }
28                 w.Stop();
29                 x2 += w.ElapsedMilliseconds;                
30 
31 
32                 w.Start();
33                 for (int i = 0; i < 100000000; i++)
34                 {
35                     if (!a.Equals(b))
36                         a = b;
37                 }
38                 w.Stop();
39                 x3 += w.ElapsedMilliseconds;                    
40             }
41 
42             System.Diagnostics.Debug.WriteLine(String.Format("直接赋值耗时:{0}ms", x1));
43             System.Diagnostics.Debug.WriteLine(String.Format("判断赋值耗时:{0}ms", x2));
44             System.Diagnostics.Debug.WriteLine(String.Format("判断赋值2耗时:{0}ms", x3));

运行结果:

直接赋值耗时:3294ms   
判断赋值耗时:5955ms
判断赋值2耗时:18244ms

结论:

判断后严重影响性能,所以无需判断,直接赋值。