C#编译器对局部变量的优化指南
作者:冠军 时间:2023-06-23 14:54:12
前言
C# 的编译器可以对代码进行优化,所以,我们在写代码的时候,可以更多地考虑一下代码的易读性问题。
不考虑基本的对齐和换行美化。看一下局部变量优化问题。
C# 示例代码
例如,我们有一段如下的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var s = DoSomething();
Console.WriteLine(s);
}
static string DoSomething()
{
var s1 = "Hello, world.";
var s2 = s1.ToUpper();
return s2;
}
}
}
在 DoSomething() 这个方法中,里面定义了两个局部变量:
s1
s2
在 Main() 方法中,定义了一个局部变量:
s
定义 s1 和 s2 是为了提高代码的可读性,它们会导致生成冗余的代码,降低执行效率吗?
我们分别在 Debug 模式下和 Release 模式下进行编译,使用 ILDasm 查看生成的中间代码。
Debug 模式下生成的中间代码
在 Debug 下编译之后,DoSomething() 生成的中间代码如下,可以看到实际上有 3 个局部变量。除了我们自己定义的 s1 和 s2 之外,还有一个生成的 V_2,代码的尺寸为 20。
.method private hidebysig static string DoSomething() cil managed
{
// Code size 20 (0x14)
.maxstack 1
.locals init ([0] string s1,
[1] string s2,
[2] string V_2)
IL_0000: nop
IL_0001: ldstr "Hello, world."
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: callvirt instance string [mscorlib]System.String::ToUpper()
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: stloc.2
IL_0010: br.s IL_0012
IL_0012: ldloc.2
IL_0013: ret
} // end of method Program::DoSomething
看一下 Main() 方法。
有我们定义的 s 这一个局部变量,代码尺寸为 15 个字节。
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 15 (0xf)
.maxstack 1
.locals init ([0] string s)
IL_0000: nop
IL_0001: call string ConsoleApp1.Program::DoSomething()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call void [mscorlib]System.Console::WriteLine(string)
IL_000d: nop
IL_000e: ret
} // end of method Program::Main
Release 模式下生成的中间代码
而在 Release 模式下,实际上,DoSomething() 中所有的局部变量都被优化掉了。代码尺寸也只有 11 个字节。
.method private hidebysig static string DoSomething() cil managed
{
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello, world."
IL_0005: callvirt instance string [mscorlib]System.String::ToUpper()
IL_000a: ret
} // end of method Program::DoSomething
还可以看一下 Main() 方法,这个局部变量 s 也被优化掉了。代码尺寸也只有 11 字节了。
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: call string ConsoleApp1.Program::DoSomething()
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Program::Main
结论
编译器会尽可能对代码进行优化,我们可以为了提高代码的易读性增加一些局部变量,这并不会导致生成冗余代码并导致执行性能的下降。
来源:https://www.cnblogs.com/haogj/p/13889976.html
标签:c#,局部,优化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于EntityWrapper的in用法
2023-11-29 09:02:11
![](https://img.aspxhome.com/file/2023/3/60353_0s.png)
java中Swing会奔跑的线程侠
2021-12-14 23:47:36
![](https://img.aspxhome.com/file/2023/8/65158_0s.gif)
MyBatis 动态拼接Sql字符串的问题
2021-08-09 05:23:46
C语言实现Flappy Bird小游戏
2022-03-13 13:56:24
解读在C#中winform程序响应键盘事件的详解
2023-10-08 09:17:00
c++异常处理机制示例及详细讲解
2022-04-27 11:31:27
SpringBoot自定义Starter实现流程详解
2022-05-19 07:03:25
![](https://img.aspxhome.com/file/2023/1/66071_0s.png)
Winform在DataGridView中显示图片
2023-06-26 21:02:48
SpringBoot项目中使用Mockito的示例代码
2023-09-25 22:31:14
![](https://img.aspxhome.com/file/2023/3/62373_0s.png)
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
java IO实现电脑搜索、删除功能的实例
2021-12-17 07:16:14
springboot项目中使用Swagger的简单示例
2023-01-14 05:18:24
![](https://img.aspxhome.com/file/2023/9/62989_0s.png)
SpringBoot的@Value给静态变量注入application.properties属性值
2023-09-20 11:54:07
Android RecyclerView加载不同布局简单实现
2022-07-15 16:57:09
![](https://img.aspxhome.com/file/2023/9/139219_0s.gif)
SpringBoot实用小技巧之如何动态设置日志级别
2023-02-09 04:27:58
![](https://img.aspxhome.com/file/2023/1/94311_0s.png)
Android gradle打包并自动上传的方法
2022-01-23 06:50:37
![](https://img.aspxhome.com/file/2023/9/138909_0s.jpg)
C#采用OpenXml给Word文档添加表格
2023-03-10 14:07:13
![](https://img.aspxhome.com/file/2023/8/108638_0s.jpg)
Android中使用TextView实现文字跑马灯效果
2023-07-28 11:07:33
![](https://img.aspxhome.com/file/2023/9/84729_0s.gif)
Android仿美团淘宝实现多级下拉列表菜单功能
2022-07-24 18:42:18
![](https://img.aspxhome.com/file/2023/4/137114_0s.png)
java显示当前运行时的参数(java运行参数)
2023-09-07 10:03:22