C#整形转为字符串(c#整型转字符)

C#整形转为字符串(c#整型转字符)

编码文章call10242025-02-01 3:40:0315A+A-

前言

群里有人在说int转string,两种方式那种要好一些.当时心理就觉得调用ToString这种方式要一些.至于为什么直接调用类型ToString函数会好一些却说不出个所以然来.

下面看一下这两种方式:

  1. 123.ToString()
  2. 123 + ""

当我们评测代码性能好不好的时候,是不能凭直觉去断言的,是需要工具测试代码的,在.Net下BenchmarkDotNet就是很好的工具.

[Benchmark] 
public string ToString12345() => 12345.ToString();

[Benchmark]
public string ToString54321() => 12345 + "";

测试结果:

根据性能测试,两种用法性能在同一.Net版本性能相差不大,当然在.Net Core 3.1的是int+""性能要好一些.不过也看出在.Net 5性能改进很大.

C#编译后的代码:

[Benchmark]
public string ToString12345()
{
    return this.val1.ToString();
}

[Benchmark]
public string ToString54321()
{
    return this.val2.ToString() ?? "";
}

看JIT生成的汇编代码:

//ToString12345
C.ToString12345()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: sub esp, 0xc
    L0006: xor eax, eax
    L0008: mov [ebp-8], eax
    L000b: mov [ebp-0xc], eax
    L000e: mov [ebp-4], ecx
    L0011: cmp dword ptr [0x5fac1a8], 0
    L0018: je short L001f
    L001a: call 0x04326120
    L001f: mov dword ptr [ebp-8], 0x3039
    L0026: lea ecx, [ebp-8]
    L0029: call System.Int32.ToString()
    L002e: mov [ebp-0xc], eax
    L0031: mov eax, [ebp-0xc]
    L0034: mov esp, ebp
    L0036: pop ebp
    L0037: ret

//ToString54321    
C.ToString54321()
    L0000: push ebp
    L0001: mov ebp, esp
    L0003: sub esp, 0x10
    L0006: xor eax, eax
    L0008: mov [ebp-8], eax
    L000b: mov [ebp-0xc], eax
    L000e: mov [ebp-0x10], eax
    L0011: mov [ebp-4], ecx
    L0014: cmp dword ptr [0x5fac1a8], 0   //判断是否为空
    L001b: je short L0022
    L001d: call 0x04326120
    L0022: mov dword ptr [ebp-8], 0xd431
    L0029: lea ecx, [ebp-8]
    L002c: call System.Int32.ToString()
    L0031: mov [ebp-0xc], eax
    L0034: mov eax, [ebp-0xc]
    L0037: mov [ebp-0x10], eax
    L003a: cmp dword ptr [ebp-0xc], 0
    L003e: jne short L0049
    L0040: mov eax, [0x19b42038]
    L0046: mov [ebp-0x10], eax
    L0049: mov eax, [ebp-0x10]
    L004c: mov esp, ebp
    L004e: pop ebp
    L004f: ret

从汇编代码看出,int+""生成汇编代码,还是加了判断是否为空,不为空的时候才会调用ToString方法,是比直接调用ToString多一个判断.

.Net 5/6/7 性能基准测试对比(2022.03.26增加)

刚才找了一下Int32的ToString源码,发现2020年后,就没进行调整了.

个人能力有限,如果您发现有什么不对,请私信我

如果您觉得对您有用的话,可以点个赞或者加个关注,欢迎大家一起进行技术交流

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4