compiler-construction


Which is faster: ldc.i4/ldstr or ldloc?


I'm writing a very basic compiler for the .NET platform and would like to know something for my implementation of constants.
In my compiler, the use of constants will hopefully replace the ldloc operation.
If the constants theAge(18) and theName(Barry) were actually variables, the IL might be something like:
ldstr "Your name is "
ldloc theName
concat //Example operation for concatenating
ldstr " and you are "
concat
ldloc theAge
tostring //Example operation for changing the variable to a string
concat
But if I implemented constants:
ldstr "Your name is "
ldstr "Barry"
concat
ldstr " and you are "
concat
ldc.i4 18
tostring
concat
Which is faster: ldc.i4/ldstr or ldloc? Or is it better to store the constants as variables?
I agree with #500's comment: this is a microoptimization, you should worry about this only after your compiler is working. And then, you should use benchmarks to figure out which options is faster and by how much, performance is notoriously hard to predict.
If I was to try to predict this (despite my own warning above), I would say that loading constants directly is going to be faster (if the difference is actually going to be measurable).
This is because in the ldc option, the CPU will read the instruction and then can directly write the value to a register. With ldloc, it will also load the value from the stack. The situation with ldstr is similar.
But if the value in the local variable is effectively constant, the JIT compiler could optimize ldloc to the same code as ldc, so there might not be any difference at all. (But I don't know if the common JIT compilers can do that.)

Related Links

How is compare different from compareTo in this Scala code?
What do you think about interpreter of usually compiled languages?
Implementation of ll(k) to ll(1) convertor !
What are the highest level languages that can be compiled?
Unreachable code: error or warning?
does assembler output differ between operating systems?
Anywhere I can find good LR(1) and LALR(1) state generation examples or reading material?
How can I modify the text of tokens in a CommonTokenStream with ANTLR?
Understanding compiler error messages [closed]
Generating Assembly For an x86 Processor
Building interference graph for register allocation
GLR parsing algorithm resources
Do any languages have neither an interpreter nor a compiler?
How to parse an if statement in bison
not clear with the job of the linker
What happens to identifiers in a program?

Categories

HOME
wso2-am
minimum-spanning-tree
push-notification
raspberry-pi
sd-card
bpmn
react-redux
enterprise-library-5
synchronization
primary-key
vifm
midi
postgres-xl
windows-azure-storage
spring-tool-suite
serverless-framework
lombok
windows-7-x64
propel
lucene.net
plunker
amazonsellercentral
trading
cas
wpfdatagrid
strncpy
publish
replaceall
ejabberd-module
dbclient
atlassian-crucible
quote
instant-messaging
uisplitview
noraui
catalog
host
wixsharp
plsql-psp
space-complexity
greenrobot-eventbus
squib
preconditions
galen
dynamic-reports
node-sass
apple-news
qwt
jsch
knockout-components
kbuild
pg-dump
spring-mongodb
color-picker
removechild
master-slave
suffix-tree
unity-networking
r-tree
marching-cubes
simple-framework
iis-arr
dukescript
apache-commons-fileupload
fluid-dynamics
unity3d-gui
c++03
directoryservices
javax.mail
python-green
starcluster
poller
balanced-payments
stxxl
gridfs
cos
jelly
cisco-jtapi
buildr
spring-portlet-mvc
removeclass
heartbeat
f#-powerpack
xtype
winbugs14
google-friend-connect
web-application-design
revisions
suppress
user-friendly
genealogy
django-notification

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