Home All Groups Group Topic Archive Search About

Enum vs Constants performance

Author
15 Sep 2005 4:36 AM
farseer
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?

Author
15 Sep 2005 5:34 AM
Mattias Sjögren
>if an enum requires boxing often, i'd assume constants would win on
>performance, is that true?

Why would enums require boxing more often than constant integer values
(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.
Are all your drivers up to date? click for free checkup

Author
15 Sep 2005 5:46 AM
Jon Skeet [C# MVP]
farseer <fars***@optonline.net> wrote:
> 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?

Firstly, as Mattias says, there's no more boxing involved with enums
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
Author
15 Sep 2005 5:18 PM
John Mark Howell
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?
>

Bookmark and Share