constants


Best way to define a constant in function/task scope


In my function (or task), I have a constant string that is only used inside that method.
What is the best way to define it (for performance):
const static string stuff = "stuff";
const string stuff = "stuff";
static string stuff = "stuff";
string stuff = "stuff";
Example on EDA Playground: http://www.edaplayground.com/s/4/1090
const will prevent future writes, IEEE std 1800-2012 § 6.20.6 "Const constants" states "... a const can be set during simulation ..." which suggests that it is up to the vender to decide if there should be any optimization for performance.
static will put the variable into shares memory. It could help or hurt performance depending on the simulation scenario. Actual performance impact is simulator specific, so you will need to run your own benchmarks. IEEE std 1800-2012 § 6.21 "Scope and lifetime" for more.
For a small project, the performance impact will be negligible. For large projects, performance needs to be split into categories: memory usage, and memory access time. static variables can have trade-off smaller memory footprint (shared memory) and longer look-up times (memory address of the static variable can be faraway for the rest of the object data). const is unlikely to add any negative performance.
Easiest way to get some basic performance data is to end the simulation with $finish(2). See IEEE std 1800-2012 Table 20-1—Diagnostics for $finish. This will report the simulation time, location, and statistics about the memory and CPU time used in simulation if the simulator is following the standard.
With the provided example using ModelSim 10.1d, all combinations reported the same memory usage. Run time was only effected by the number of calls the print method, not the const/static attribute.
If I had to guess, the performance would be (ordered best to worse):
const string in a static method
string in a static method
const static string
static string
const string
string

Related Links

Instrumenting bytecode at method level
async_work_group_copy from __constant
How to use Pascal Inno Setup ExpandConstant with a string variable?
Why are constants in C-header files of libraries always defined as hexadecimal?
DirectX11 set shader constants
wampserver configuration - how to enable constants?
How do I use a constant to specify a File path in NSIS?
What ID values are constant to all Sitecore installations?
Php Notice: Use of undefined constant
fcntl.h doesn't contain all status flag constants
Choosing values for constants
How to manage Constants in Application
PHP Magic Constants
Using constants for message keys and database table names and column names
Why do we always declare constants to be powers of 2?
Weird constants [closed]

Categories

HOME
cakephp-3.x
plsql
tinyos
boost-thread
parse-server
travis-ci
zend-framework2
heap-memory
adsense
value
gspread
user
sequelize.js
google-spreadsheet-api
contact-form-7
slim-3
vuex
hosts
iso
google-shopping
future
glpk
line-api
pdo
titan
apache-karaf
movies
uiautomator
nsmenuitem
cocoa-touch
searchbar
tfs2013
fabric
mef2
upnp
xilinx-ise
positioning
blackboard
piecewise
helix-3d-toolkit
modelandview
web-audio-api
transient
modulo
powershell-remoting
jboss-esb
sendgrid-templates
estimation
rapidweaver
recycle-bin
chunked-encoding
jags
istorage
spell-checking
struts-layout
crystal-reports-8.5
tcserver
instruction-set
communication-protocol
onresume
tomee
debugdiag
excon
uiprogressview
qregexp
spring-lemon
codesign
mongo-c-driver
asp.net-mvc-partialview
inet
proxygen
group-concat
vips
datasnap
js-cookie
windowlistener
programming-paradigms
ibm-data-studio
subversion-edge
proximity
grunt-contrib-concat
firebug-lite
reserved-words
mimosa
bsod
magicalrecord-2.2
java.util.date
appfog
magickwand
prng
angularjs-timeout
zend-framework-modules
tridion2009
bulbs
socketstream
cadisplaylink
v4l
hashalgorithm
querystringparameter
goliath
gtk2hs
libavformat
microsoft.ink
kpi
inline-if
evb

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App