如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数? [英] How Can I Call A Function That Measures Time Of Any Given Method With Just The Helpo Of Attributes To Reduce Loc?

查看:98
本文介绍了如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数使用秒表实例通过启动和停止来测量方法的时间.我可以以某种方式在一个属性内定义该函数,然后用该属性修饰任何给定的方法以测量该方法的时间吗?这将减少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应用于属性类,并指定此属性目标.它将允许您的属性的用户将其应用于任何方法.请参阅:
https://msdn.microsoft.com/en-us/library/system.attributeusageattribute%28v = vs.110%29.aspx [ https://msdn.microsoft.com/en-us/library/system.attributetargets%28v = vs.110%29.aspx [ https://msdn.microsoft.com/en-us/library/aa288454%28v = vs.71%29.aspx [ ^ ],
https://msdn.microsoft.com/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:
https://msdn.microsoft.com/en-us/library/system.attributeusageattribute%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.attributetargets%28v=vs.110%29.aspx[^].

The attribute and its properties can be retrieved using reflection. Please see:
https://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx[^],
https://msdn.microsoft.com/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


这篇关于如何调用仅通过属性帮助减少位置来测量任何给定方法时间的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆