473,387 Members | 1,745 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,387 software developers and data experts.

constants vs. readonly-fields

Hi all,

Can there be a performance difference, if i use readonly fields instead of
constants?

e.g.:
const int n = 100;
vs.
static readonly int = 100;

or
const string s = "text";
vs.
static readonly string s = "text";

Christof
Nov 17 '05 #1
11 1653
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Can there be a performance difference, if i use readonly fields instead of
constants?

e.g.:
const int n = 100;
vs.
static readonly int = 100;

or
const string s = "text";
vs.
static readonly string s = "text";


I haven't profiled it first hand, but I'd suggest that *if* there's any
difference, the const will be faster since it is compile time initialised,
whereas a static read only field is runtime initialised (as it can be
assigned by static constructors.).

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
"Tim Haughton" <ti*********@gmail.com> schrieb im Newsbeitrag
news:Dk*******************@fe08.news.easynews.com. ..
"Christof Nordiek" <cn@nospam.de> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Can there be a performance difference, if i use readonly fields instead
of
constants?

e.g.:
const int n = 100;
vs.
static readonly int = 100;

or
const string s = "text";
vs.
static readonly string s = "text";


I haven't profiled it first hand, but I'd suggest that *if* there's any
difference, the const will be faster since it is compile time initialised,
whereas a static read only field is runtime initialised (as it can be
assigned by static constructors.).

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Thanks for answering.
Your talking about initialization.
But what about accessing those fields/constants.

Christof
Nov 17 '05 #3
> Thanks for answering.
Your talking about initialization.
But what about accessing those fields/constants.


I can't see there being a reason for any discrepency in access times. Once
they're created, they're just members that can't be touched.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #4
Christof,

Accessing n will be faster, because when you compile an assembly with a
reference to the assembly containing that constant, that value is
substituted into the code. With readonly fields, you have to actually do a
lookup.

I hope you aren't trying to do this in the hopes of optimizing your
code. It sounds premature, unless you have some performance numbers to back
it up otherwise.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Christof Nordiek" <cn@nospam.de> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl...
Hi all,

Can there be a performance difference, if i use readonly fields instead of
constants?

e.g.:
const int n = 100;
vs.
static readonly int = 100;

or
const string s = "text";
vs.
static readonly string s = "text";

Christof

Nov 17 '05 #5


Christof Nordiek wrote:
Hi all,

Can there be a performance difference, if i use readonly fields instead of
constants?


Try measuring the performance of your program with a profiler instead of
focusing on small things like this.

It will probably not make much (performance) difference if your program
uses a readonly field or a constant.

Before you use performance as the reason for syntactic choices you
should verify that performance affected by this choice is an issue.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e6**************@TK2MSFTNGP12.phx.gbl...
Christof,

Accessing n will be faster, because when you compile an assembly with a reference to the assembly containing that constant, that value is
substituted into the code. With readonly fields, you have to actually do a lookup.


Hi Nicholas, I hadn't realised that this was what the compiler did. How does
this work if the version of the referenced assembly changes, by policy or
other means? Say, for example, the constant is a different value in the new
version of the referenced assembly, how does this new version percolate
through to the client without a rebuild?

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #7

"Tim Haughton" <ti*********@gmail.com> wrote in message
news:kz*******************@fe08.news.easynews.com. ..
Say, for example, the constant is a different value in the new
version of the referenced assembly, how does this new version percolate
through to the client without a rebuild?


It would not. You need a rebuild of all calling assemblies. Who are they?
This is one of the dangers with constants. Why I would never sport a public
constant. I'd rather go for static readonly

- Michael S

Nov 17 '05 #8
> It would not. You need a rebuild of all calling assemblies. Who are they?
This is one of the dangers with constants. Why I would never sport a public constant. I'd rather go for static readonly


That's an interesting design decision by Microsoft. I wonder if the
motivation was performance. It seems to be a tiny performance gain for a
non-trivial deployment consideration.

But like you say, I don't think I've ever delivered anything with a public
const field so it's unlikely to become an issue.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #9
Another comment on constants.
Just use them for true constants

const double PI = 3.14; // Good. Won't change, except very close to the
apocalypse. System will survive the universe.
const int CustomerNameLength = 30; // Bad. This number may change when
requirements changes.

- Michael S
Nov 17 '05 #10
"Michael S" <a@b.c> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
Another comment on constants.
Just use them for true constants

const double PI = 3.14; // Good. Won't change, except very close to the
apocalypse. System will survive the universe.
const int CustomerNameLength = 30; // Bad. This number may change when
requirements changes.


You're making gross assumptions about the absence of space time variations
and adherence to Euclidean geometry. Although, I'll grant you, if there was
sufficient local space time curvature giving rise to such deviations from
Euclidean geometry, the backwards compatibility of my software would
probably be very low on my agenda.

;¬)

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #11

"Tim Haughton" <ti*********@gmail.com> wrote in message
news:yU********************@fe05.news.easynews.com ...
"Michael S" <a@b.c> wrote in message
news:ul**************@TK2MSFTNGP09.phx.gbl...
Another comment on constants.
Just use them for true constants

const double PI = 3.14; // Good. Won't change, except very close to the
apocalypse. System will survive the universe.
const int CustomerNameLength = 30; // Bad. This number may change when
requirements changes.


You're making gross assumptions about the absence of space time variations
and adherence to Euclidean geometry. Although, I'll grant you, if there
was
sufficient local space time curvature giving rise to such deviations from
Euclidean geometry, the backwards compatibility of my software would
probably be very low on my agenda.

;¬)


LOL!

And considering that most computers would not like 'deviations from
Euclidean geometry' I think we can safely write this off as a Hardware
Problem =)

- Michael S
Nov 17 '05 #12

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

Similar topics

8
by: Mark Rae | last post by:
Hi, Another stupid newbie question from me, I'm sorry to say... but can anyone tell me how to simulate the concept of a global constant in a C# Windows app? The app in question contains...
3
by: ESPNSTI | last post by:
Hi, I'm looking for a way to associate multiple "lists" of constants to each other. For example, I'd like a way to look up a database column's name based on its "number" in a constants list. ...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
1
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I...
4
by: Shayne H | last post by:
What is the best way to enumerate a grouping of strings? The way I have been doing it is: Public Enum PlatformID Unknown Win16 Win32 Win32NT WinCE End Enum
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
29
by: Michael D. Ober | last post by:
Is there any way to create a constant in a class that can be used both with an instantiated object and without. For example: dim ClassConst as string = myClass.ConstantString dim myObj = new...
4
by: Dave | last post by:
If I have some constants I want to use throughout my application, how should I implement that? const string USA = "US" const string CANADA = "CA" etc.
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
1
by: paul.hester | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
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: 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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.