473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Enum vs Constants performance

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?

Nov 17 '05 #1
3 7231
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.
Nov 17 '05 #2
farseer <fa*****@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
Nov 17 '05 #3
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}
"farseer" <fa*****@optonline.net> wrote in message news:11**********************@g14g2000cwa.googlegr oups.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?

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Russell Shaw | last post by:
Hi, gcc-3.4 complains about non-integers in: enum {IDENTIFIER = "<identifier>", WIDGETDEF = "widgetdef"}; even if i cast the strings to integers.
12
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
2
by: =?Utf-8?B?UmljaGFyZCBUb2NjaQ==?= | last post by:
I'm learning Visual Basic using the .Net Framework 2.0. I have been playing with consuming web services in applications. I am using a free web service that gives me information for currency...
2
by: Daniel Jeffrey | last post by:
Hello. Can anyone help me please. I have created an Enumeration, and I loop through it, it all works ok, except the first item returns 2 times. Code is below. When called I Get "Text Edit" 2...
7
by: Travis | last post by:
I'm curious, if you have an enum say... enum Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; I understand the default will be Mon=0, Tue=1, Wed=2, etc. What I'm curious about is if there is a...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.