C#整形转为字符串

起因

群里有人在说int转string,两种方式那种要好一些.当时心理就觉得调用ToString这种方式要一些.至于为什么直接调用类型ToString函数会好一些呢?
  1. 123.ToString()
  2. 123 + ""
当我们评测代码性能好不好的时候,是不能凭直觉去断言的,是需要工具测试代码的,在.Net下BenchmarkDotNet就是很好的工具.
[Benchmark] 
public string ToString12345() => 12345.ToString();

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

测试结果:

在c#类型转字符串,直接调用ToString
根据性能测试,两种用法性能在同一.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生成的汇编代码:

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

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增加)

在.Net 6/.Net 7性能基准测试
刚才找了一下Int32的ToString源码,发现2020年后,就没调整了.
在.Net 6/.Net 7 Int32源码
秋风 2021-01-09