c# - Advantage of using CustomAttributes vs GetCustomAttributes() -
i noticed today new properties had appeared in intellisense on system.type
object .net 4.5 projects. among these 1 called customattributes
.
i intrigued since had understood getcustomattributes
1 of expensive reflection calls (dynamicinvoke
, aside, of course). understand it, every call getcustomattributes
results in calling constructors attributes (and memory allocation). i've resorted caching custom attributes separately avoid performance bottlenecks when processing large numbers of types , such.
so, wrote test see if customattributes
more performant getcustomattributes
:
static void main(string[] args) { var sw = stopwatch.startnew(); debug.writeline(typeof(attributed).gettype()); (int = 0; < 10000; i++) { var attrs = typeof(attributed) .customattributes .select(a => a.attributetype) .tolist(); } sw.stop(); debug.writeline("using .net 4.5 customattributes property: {0}", sw.elapsed); sw = stopwatch.startnew(); (int = 0; < 10000; i++) { var attrs = typeof(attributed) .getcustomattributes(true) .select(a => a.gettype()) .tolist(); } sw.stop(); debug.writeline("using getcustomattributes method: {0}", sw.elapsed); }
with test classes:
[dummy] [dummy] [dummy] [dummy] [dummy] [dummy] class attributed { } [attributeusage(attributetargets.class, allowmultiple=true)] class dummyattribute : attribute { public dummyattribute() { } }
the results surprising:
system.runtimetype using .net 4.5 customattributes property: 00:00:00.1351259 using getcustomattributes method: 00:00:00.0803161
the new customattributes
property slower existing getcustomattributes
method!
debugging further, discovered attribute constructors not called iterating customattributes
(which expected since looks reading metadata). yet somehow, slower getcustomattributes
calls constructors.
my question
personally think more readable use new property, cost 1.5x-ish slower performance.
so, advantage there, if any, of using customattributes
instead of getcustomattributes()
?
i'm assuming situation checking see if attribute of type exists on clas...not using methods or properties on instance of attribute.
you making traditional benchmarking mistake, 1 makes many .net programmers think reflection slow. slower is. reflection lazy, don't pay when don't use it. makes first measurement include cost of page-faulting metadata ram , setup type info reflection cache. cost not included in second measurement, making getcustomattributes() better is.
always include loop around benchmark code, run 10 times. you'll see customattributes property isn't that slow, measure @ (roughly) 0.083 vs 0.063 seconds, ~30% slower.
the customattributes property needed added in .net 4.5 support language projection winrt. cannot use getcustomattributes() in store, phone or pcl project. reflection very different in winrt, inevitable side-effect of being com based api. implementation code enough make anyone's eyes bleed broad outline property implemented in c# , method implemented in clr. c# code needs more work handle language projection details inevitably slower.
so keep using getcustomattributes(), use property when have to. 30% speed difference isn't otherwise drastic reason compromise style , readability, apply common sense.
Comments
Post a Comment