从减少装箱次数出发提升代码性能
2018-01-05 10:24
447 查看
测试tostring是否装箱的代码 结论tostring方法并不发生装箱,可利用此减少装箱次数,进而减少内存分配次数,降低GC工作量,最终提升代码性能 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace boxing { class Program { static void Main(string[] args) { //testboxing Console.WriteLine(string.Format("{0}:{1}", 4, 5)); Console.WriteLine("----------"); //testnoboxing var result = 1.ToString() + ":"+2.ToString(); Console.WriteLine(result); } } } 其对应的IL指令 .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 71 (0x47) .maxstack 3 .locals init ([0] int32 V_0) IL_0000: ldstr "{0}:{1}" IL_0005: ldc.i4.4 IL_0006: box [mscorlib]System.Int32 IL_000b: ldc.i4.5 IL_000c: box [mscorlib]System.Int32 IL_0011: call string [mscorlib]System.String::Format(string, object, object) IL_0016: call void [mscorlib]System.Console::WriteLine(string) IL_001b: ldstr "----------" IL_0020: call void [mscorlib]System.Console::WriteLine(string) IL_0025: ldc.i4.1 //将整数值 1 作为 int32 推送到计算堆栈上。 IL_0026: stloc.0 //从计算堆栈的顶部弹出当前值并将其存储到索引 0 处的局部变量列表中。 IL_0027: ldloca.s V_0 //将位于特定索引处的局部变量的地址加载到计算堆栈上(短格式)。 IL_0029: call instance string [mscorlib]System.Int32::ToString() IL_002e: ldstr ":" // IL_0033: ldc.i4.2 //将整数值 2 作为 int32 推送到计算堆栈上。 IL_0034: stloc.0 //从计算堆栈的顶部弹出当前值并将其存储到索引 0 处的局部变量列表中。 IL_0035: ldloca.s V_0 //将位于特定索引处的局部变量的地址加载到计算堆栈上(短格式)。 IL_0037: call instance string [mscorlib]System.Int32::ToString() IL_003c: call string [mscorlib]System.String::Concat(string, string, string) IL_0041: call void [mscorlib]System.Console::WriteLine(string) IL_0046: ret } // end of method Program::Main
相关文章推荐
- 数据库性能提升之减少访问数据库次数
- 数据库性能提升之减少访问数据库次数
- 数据库提升性能 要减少访问数据库次数
- 数据库性能提升之减少访问数据库次数
- 减少访问DOM的次数提升javascript性能
- javascript性能提升——减少访问DOM的次数
- 应用 memcached 提升站点性能——减少读自数据库和数据源
- Keil如何提升性能和减小代码大小
- 修改一行SQL代码 性能提升了100倍
- PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%
- ASP.NET站点性能提升-减少最后一字节的时间
- 减少oracle sql回表次数 提高SQL查询性能
- PHP开启opcache提升代码性能
- 教你50招提升ASP.NET性能(九):显式的使用using语句减少内存泄露
- java性能优化:35个小细节让你提升java代码的运行效率
- ASP.NET站点性能提升-减少长等待时间
- 减少oracle sql回表次数 提高SQL查询性能
- 减少oracle sql回表次数 提高SQL查询性能
- 代码模块化管理和提升web性能的知识点