|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Enum vs Constants performanceif an enum requires boxing often, i'd assume constants would win on
performance, is that true? Further, it appears that if you need to pass enum values to functions that accept only uint, int or byte (therefore, you also must unbox and cast the enums to the type it "inherits" from) quite often, then constants may be a better choice if performance is an issue. Are these assumptions correct? >if an enum requires boxing often, i'd assume constants would win on Why would enums require boxing more often than constant integer values>performance, is that true? (I assume thatäs the alternative you mean)? Mattias -- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. farseer <fars***@optonline.net> wrote:
> if an enum requires boxing often, i'd assume constants would win on Firstly, as Mattias says, there's no more boxing involved with enums > performance, is that true? > > Further, it appears that if you need to pass enum values to functions > that accept only uint, int or byte (therefore, you also must unbox and > cast the enums to the type it "inherits" from) quite often, then > constants may be a better choice if performance is an issue. > > Are these assumptions correct? than with value-type constants. Casting an enum to an int doesn't box or unbox it, for instance. Secondly, even if there were some slight performance improvement from using a constant compared with an enum, I'd *prove* that it was actually significant in the system before moving to a less strongly type for the sake of performance. It's quite possible for performance to an issue, but for it still not to be worth micro-optimising. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too If you're concerned about boxing, then simply define your enum of the most common type. For example:
public enum MyEnum : uint {entry1 = 1,entry2 = 2,entry3 = 3} Show quoteHide quote "farseer" <fars***@optonline.net> wrote in message news:1126758997.630530.148450@g14g2000cwa.googlegroups.com... > if an enum requires boxing often, i'd assume constants would win on > performance, is that true? > > Further, it appears that if you need to pass enum values to functions > that accept only uint, int or byte (therefore, you also must unbox and > cast the enums to the type it "inherits" from) quite often, then > constants may be a better choice if performance is an issue. > > Are these assumptions correct? >
Other interesting topics
running c# executable....
Promoting an object Return from and Exception Question [OT?] Who has .NET 1.1 and doesn't know it? How can a parent know if a child control chages ProgressBar Not Updating Question Unhandled exception project level scope declaration in C# How to Cancel a "non cancellable" Event .NET 2.0: code access security / authentication |
|||||||||||||||||||||||