检查ac#方法的速度和内存大小
有人发现这个基本测试有任何问题吗?
var GC_MemoryStart = System.GC.GetTotalMemory(true);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var test = new InquiryServiceTypeData().GetActive(true);
stopWatch.Stop();
var GC_MemoryEnd = System.GC.GetTotalMemory(true);
arrayTotal[i] = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
size[i] = GC_MemoryEnd - GC_MemoryStart;
只是试图大致估计该方法运行所需的时间以及它使用的堆中的内存量。
也就是说,有些测试使用数据集/数据表,有些测试使用列表,有些测试两者都使用。
这段代码被放入一个循环中,以便我可以更改迭代次数。然后,我显示每种方法花费的平均时间和平均大小。
编辑: GetActive 方法访问数据库并返回数据。我测量的不同方法以不同的方式处理返回的数据。
方法 1 - 将数据返回到数据集,然后转换为对象列表。 方法 2 - 返回数据读取器并转换为对象列表。 方法 3 - 前 2 种方法的合并。
制定该守则的目的是为了解决与同事之间的分歧。他认为我的代码很糟糕,因为数据读取器和转换为对象列表有大量“新”语句,这些语句给堆带来了负载。我认为他错了。所以我想向他展示指标,因为他没有听我的任何逻辑陈述。但你不能与数字争论。
Does anyone see any issues with this basic test?
var GC_MemoryStart = System.GC.GetTotalMemory(true);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var test = new InquiryServiceTypeData().GetActive(true);
stopWatch.Stop();
var GC_MemoryEnd = System.GC.GetTotalMemory(true);
arrayTotal[i] = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
size[i] = GC_MemoryEnd - GC_MemoryStart;
Just trying to get a general estimate of how long the method takes to run and how much memory in the heap it uses.
Aka, some of the tests use dataset/datatables, some use lists, and some use both.
This code is thrown in a loop so that I can change the number of iterations. I then display the average time each method takes and the average size.
EDIT: The GetActive method accesses a database and returns data. The different methods that I'm measuring process the data returned in a different way.
Method 1 - Returns a data into a dataset and then converts to an object list.
Method 2 - Returns a datareader and converts to an object list.
Method 3 - A merging of the first 2 methods.
The reason for the code is to settle a disagrement with a coworker. He thinks my code was crap because the datareader and converting to an object list has a ton of 'new' statements which places a load on the heap. I argue that he's wrong. So I want to show him metrics because he didn't listen to any of my logical statements. You can't argue with numbers though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那很好。
但是,您应该在对任何内容进行计时之前调用该方法一次,这样您就不会最终对抖动进行计时。
That's pretty good.
However, you should call the method once before you time anything so that you don't end up timing the JITter.
我建议您使用 ANTS Performance Profiler 6.3 。
它非常容易安装和使用。因此您可以快速改进您的 .NET 代码。
I recommend you to use ANTS Performance Profiler 6.3.
It is extremely easy to install and use. So you can improve your .NET code quickly.