技术控

    今日:102| 主题:49113
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Is using ‘using’ block really helpful?

[复制链接]
百里梨花 发表于 2016-10-13 10:57:03
176 7

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Introduction and Background

  So, it all began on the Facebook when I was posting a status (using Twitter) about a statement that I was feeling very bad that .NET team had left out the “Close” function while designing their .NET Core framework. At first I thought maybe everything was “managed” underground until I came up to an exception telling me that the file was already being used. The code that I was using was something like this,
  [code]if(!File.Exists("path")) { File.Create("path").Close(); }[/code]  However, “Close” was not defined and I double checked against .NET Core reference documentations too, and they suggested that this was not available in .NET Core framework. So I had to use other end… Long story short, it all went up like this,
  I am unable to understand why was “Close” function removed from FileStream. It can be handy guys, @dotnet.
  Then, Vincent came up with the idea of saying that “Close” was removed so that we all can use “using” blocks, which are much better in many cases.
   You don’t need that. It’s handier if you just put any resource that eats resources within the “using block”.  ��  
  Me:
  How does using, “using (var obj = File.Create()) { }” make the code handier?
  I was talking about this, “File.Create().Close();”
   Now, instead of this, we have to flush the stream, or perform a flush etc. But I really do love the “using block” suggestion, I try to use it as much as I can. Sometimes, that doesn’t play fair.  :wink:  
  He:
   Handier because I don’t have to explicitly call out the “Close()” function. Depends on the developer’s preference, but to me, I find “using (var obj = File.Create()) { }” handier and sexier to look at rather than the plain and flat “File.Create().Close();”. Also, it’s a best practice to “always” use the using block when dealing with objects that implements IDisposable to ensure that objects are properly closed and disposed when you’re done with it.  :wink:  
  As soon as you leave the using block’s scope, the stream is closed and disposed. The using block calls the Close() function under the hood, and the Close() calls the Flush(), so you should not need to call it manually.
  Me:
  I will go with LINQPad to see which one is better. Will let you know.
  So, now I am here, and I am going to share what I find in the LINQPad. The fact is that I have always had faith in the code that works fast and provides a better performance. He is an ASP.NET MVP on Microsoft and can be forgiven for the fact that web developers are trained on multi-core CPUs and multi-deca-giga-bytes of RAMs so they use the code that looks cleaner but… He missed the C# bytecode factor here. I am going to use LINQPad to find out the underlying modifications that can be done to find out a few things.
   Special thanks to Vincent: Since a few days I was out of topics to write on, Vincent you gave me one and I am going to write on top of that debate that we had.
   Notice: I also prefer using the “using” block in almost every case. Looks handier, but the following code block doesn’t look handier at all,
  [code]using (var obj = File.Create("path")) { }[/code]  And this is how it began…
  Exploring the “using” and simple “Close” calls

  LINQPad is a great software to try out the C# (or .NET framework) code and see how it works natively, it lets you see the native bytecode of ,NET framework and also lets you perform and check the tree diagrams of the code. The two types that we are interested in are, “using” statement of .NET framework and the simple “Close” calls that are made to the objects to close their streams.
  I used a Stopwatch object to calculate the time taken by the program to execute each task, then I match the results of each of the program with each other to find out which one went fast. Looking at the code of them both, it looks the thousand-feet high view of them both looks the same,
  [code]// The using block
using (var obj = File.Create("path")) { }

// The clock method
File.Create("path").Close();[/code]  They both the same, however, their intermediate code shows something else.
  [code]// IL Code for "using" block
IL_0000: nop
IL_0001: ldstr "F:/File.txt"
IL_0006: call System.IO.File.Create
IL_000B: stloc.0 // obj
IL_000C: nop
IL_000D: nop
IL_000E: leave.s IL_001B
IL_0010: ldloc.0 // obj
IL_0011: brfalse.s IL_001A
IL_0013: ldloc.0 // obj
IL_0014: callvirt System.IDisposable.Dispose
IL_0019: nop
IL_001A: endfinally
IL_001B: ret

// IL Code for the close function
IL_0000: nop
IL_0001: ldstr "F:/File.txt"
IL_0006: call System.IO.File.Create
IL_000B: callvirt System.IO.Stream.Close
IL_0010: nop
IL_0011: ret[/code]  Oh-my-might-no! There is no way, “using” block could have ever won with all of that extra intermediate code for the .NET VM to execute before exiting. The time taken by these commands was also tested and for that I used the native Stopwatch object to calculate the “ticks” used, instead of the time in milliseconds by each of the call. So my code in the LINQPad looked like this,
  [code]void Main()
{
   Stopwatch watch = new Stopwatch();
   watch.Start();
   using (var obj = File.Create("F:/file.txt")) { }
   watch.Stop();
   Console.WriteLine($"Time required for 'using' was {watch.ElapsedTicks}.");

   watch.Reset();
   watch.Start();
   File.Create("F:/file.txt").Close();
   watch.Stop();
   Console.WriteLine($"Time required for 'close' was {watch.ElapsedTicks}.");
}[/code]  The execution of the above program always results in a win for the “Close” function call. In sometimes it was a close result, but still “Close” function had a win over the “using” statement. The results are shown the images below,
   
Is using ‘using’ block really helpful?-1 (understand,everything,framework,Facebook,something)


Is using ‘using’ block really helpful?-2 (understand,everything,framework,Facebook,something)

12下一页
友荐云推荐




上一篇:A Quick Understanding of REST
下一篇:BinaryTree:学习二叉树的Python库
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

廖子华 发表于 2016-10-24 05:37:58
十分赞同楼主!
回复 支持 反对

使用道具 举报

shennianxu 发表于 2016-11-14 13:47:47
好贴,绝对要支持下
回复 支持 反对

使用道具 举报

ycx7758521 发表于 2016-11-16 17:59:41
看帖要回,回帖才健康,我使劲踩,楼主辛苦了!
回复 支持 反对

使用道具 举报

63785863 发表于 2016-11-17 16:42:19
男女关系处理得好会传出佳话,处理得不好会传出闲话。
回复 支持 反对

使用道具 举报

chenbang879 发表于 2016-11-17 22:38:46
作为一个曾经充分理解怎么吃也不胖的瘦子,如今我总算完全的体会了一吃就胖的感悟。
回复 支持 反对

使用道具 举报

沛白 发表于 2016-11-18 22:33:54
什么啊,语文是苍老师教的吗?
回复 支持 反对

使用道具 举报

目送妳旳愛※ 发表于 2016-11-21 18:13:27
百里梨花看起来很有学问!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表