如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数?

编程入门 行业动态 更新时间:2024-10-07 14:23:39
本文介绍了如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个函数,该函数使用秒表实例通过启动和停止来测量方法的时间.我可以以某种方式在一个属性内定义该函数,然后用该属性修饰任何给定的方法以测量该方法的时间吗?这将减少LOC.我不想使用任何第三方库.

I have a function that uses an instance of stopwatch to measure the time of method by starting and stopping. Can I somehow define that function inside an attribute and just decorate any given method with that attribute to measure time of that method ? That would reduce the LOC. I don''t want to use any third party library.

推荐答案

是的,可以,但是没有这样的预定义属性.您可以轻松地自己定义它并应用于要计时的功能.请注意枚举成员System.AttributeTargets.Method.定义属性时,可以将属性System.AttributeUsageAttribute应用于属性类,并指定此属性目标.它将允许您的属性的用户将其应用于任何方法.请参阅: msdn.microsoft/en-us/library/system.attributeusageattribute%28v = vs.110%29.aspx [ ^ ], msdn.microsoft/en-us/library/system.attributetargets%28v = vs.110%29.aspx [ ^ ]. 可以使用反射来检索属性及其属性.请参阅: msdn.microsoft/en-us/library/aa288454%28v = vs.71%29.aspx [ ^ ], msdn.microsoft/zh-我们/library/system.attribute.getcustomattributes%28v=vs.110%29.aspx [ ^ ]. 但是现在,您将必须自己开发用于方法计时的通用代码.创建这样的代码来计时一些静态的无参数方法并不是很容易的,这些方法不需要任何准备就可以被调用.在实际的开发实践中,这很少是重要的情况.通常,您需要测试时序如何取决于不同的方法参数或方法的声明类型的状态,尤其是在该方法是实例方法时,但是这种复杂性通常甚至适用于静态方法.仅将一个或另一个方法与另一个代码隔离开来计时是不够的.方法的性能取决于许多因素.并且没有一种通用的方法可以仅在属性中描述测试方案.为此,属性的表达能力太低,现实场景设置太复杂. 我开始用没有这样的预定义属性"回答您的问题.现在大概我可以解释为什么了.我认为您的想法并非真正富有成果.不好的主意,告诉你真相.您定义属性并根据该属性开发一些测试/计时代码.那又如何?它如何帮助您计时一些真正有用的东西?通常,您可以使用一个或另一个代码 profiler ,或者只是临时地按当前需要的方式对一些代码片段进行计时.您的想法没有显示出任何值得打扰的通用方式. 但是,当然,如果您描述了如何设计整个系统的一些有趣的细节,我会很乐意承认我的错误.但我认为这是不可能的.
—SA
Yes, you can, but there is no such predefined attribute. You can easily define it yourself and apply to the functions to be timed. Note the enumeration member System.AttributeTargets.Method. When you define your attribute, you can apply the attribute System.AttributeUsageAttribute to your attribute class, and specify this attribute target. It will allow the user of your attribute to apply it to any methods. Please see: msdn.microsoft/en-us/library/system.attributeusageattribute%28v=vs.110%29.aspx[^], msdn.microsoft/en-us/library/system.attributetargets%28v=vs.110%29.aspx[^]. The attribute and its properties can be retrieved using reflection. Please see: msdn.microsoft/en-us/library/aa288454%28v=vs.71%29.aspx[^], msdn.microsoft/en-us/library/system.attribute.getcustomattributes%28v=vs.110%29.aspx[^]. But now, you will have to develop the universal code for method timing by yourself. It would not be quite easy to create such code to time some static parameterless methods which do not require any preparation before they can be called. In real development practice, this is rarely the important case. Usually, you need to test how timing depends on different method parameters or the state of the declaring type of the method, especially when the method is the instance method, but such complications, generally, are applied even to the static methods. It''s not enough to time one or another method in isolation from the other code. The method performance depends on many factors; and there is no a universal way to describe the testing scenario just in an attribute. The expressive capacity of attributes are too low for that, and the real-life scenario settings are too complicated. I started answering your question with "there is no such predefined attribute". Probably now I can explain why. I think your idea is not really fruitful. Bad idea, to tell you the truth. You define the attribute and develop some testing/timing code based on it. Than what? How can it help you to time something really useful? Normally, you either use one or another code profiler, or just time some fragments of code on ad-hoc basis, the way you need it at the moment. Your idea does not show any universal way which would worth bothering. But of course, if you describe some interesting detail on how the whole system can be designed, I''ll gladly admit my mistake. But I don''t think it''s possible.
—SA

更多推荐

如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数?

本文发布于:2023-11-30 15:34:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   测量   属性   位置   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!