Random photo
Loading...
Domains for sale
|
September 26, 2004Wesner Moise on Enums and PerformanceWesner Moise (.NET Undocumented) writes on enums perf in .NET. While enums are value types and are often recognized and treated like standard integral values by the runtime (in IL, enums and integers have almost no distinction), there are few performance caveats to using them.That's obvious, but this is not really: ToString uses reflection, the first time it is called, to dynamically retrieve enumeration constants from the enumerated type and stores those values into a hash table for future use. However, GetHashCode always uses reflection to retrieve the underlying value. While ValueType.Equals will attempt to do a fast bit check, when a valuetype with no reference methods, such as is the case for enumerated types, it won't be faster than a direct compare.And this is sad: Another ironic conclusion is that creating your own version of an enumerated type, not derived from Enum, is going to be faster than the CLR versions, because you can ensure that GetHashCode, Equals, ToString, IComparable, and IComparable<T> are not inherited from any of base classes such as ValueType.Now what? Back to Java "enums"? September 26, 2004 12:03 PM
| #.NET
Comments
Post a comment
|