我正在为 .NET 平台编写一个非常基本的编译器,并且想了解一些关于我的常量实现的信息。
在我的编译器中,常量的使用有望取代 ldloc 操作。
如果常量 theAge(18) 和 theName(Barry) 实际上是变量,则 IL 可能类似于:
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
但是如果我实现了常量:
ldstr "Your name is "
ldstr "Barry"
concat
ldstr " and you are "
concat
ldc.i4 18
tostring
concat
哪个更快:ldc.i4/ldstr 或 ldloc?还是将常量存储为变量更好?