拳击与 int 的 ToString
我有一个 .net 6 项目并写了一些日志。通常的日志看起来:
int profileId = 100; //Any number
_logger.LogInformation("profileId = {ProfileId}", profileId);
这里,发生了装箱(int -> object)。 因此,我决定将代码替换为:
int profileId = 100; //Any number
string profileIdString = profileId.ToString();
_logger.LogInformation("profileId = {ProfileId}", profileIdString);
但是,我决定运行基准测试并比较这两个选项。 令人惊讶的是,结果并不是我所期望的。 这是代码。
[MemoryDiagnoser]
public class Benchmark
{
private const int N = 1000;
[Benchmark]
public void Boxing()
{
for (var i = 0; i < N; i++)
{
var s = string.Format("Test: {0}", i);
}
}
[Benchmark]
public void CastToString()
{
for (var i = 0; i < N; i++)
{
var s = string.Format("Test: {0}", i.ToString());
}
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
输出:
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------------- |---------:|---------:|---------:|-------:|----------:|
| Boxing | 26.41 us | 0.236 us | 0.221 us | 4.8828 | 62 KB |
| CastToString | 32.91 us | 0.297 us | 0.278 us | 5.4626 | 70 KB |
看起来装箱方法效果更好:它运行得更快并且分配的内存更少。 我的问题是为什么会发生这种情况?这是否意味着在这种情况下使用拳击方法更好?或者我的性能测试写错了?
I have a .net 6 project and write some logs. The usual log looks:
int profileId = 100; //Any number
_logger.LogInformation("profileId = {ProfileId}", profileId);
Here, the boxing happens (int -> object).
So, I decided to replace the code with:
int profileId = 100; //Any number
string profileIdString = profileId.ToString();
_logger.LogInformation("profileId = {ProfileId}", profileIdString);
However, I decided to run the benchmark and compare both options.
Surprisingly, the results are not the ones I expected.
Here is the code.
[MemoryDiagnoser]
public class Benchmark
{
private const int N = 1000;
[Benchmark]
public void Boxing()
{
for (var i = 0; i < N; i++)
{
var s = string.Format("Test: {0}", i);
}
}
[Benchmark]
public void CastToString()
{
for (var i = 0; i < N; i++)
{
var s = string.Format("Test: {0}", i.ToString());
}
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
Output:
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|------------- |---------:|---------:|---------:|-------:|----------:|
| Boxing | 26.41 us | 0.236 us | 0.221 us | 4.8828 | 62 KB |
| CastToString | 32.91 us | 0.297 us | 0.278 us | 5.4626 | 70 KB |
It looks like the boxing approach works better: It runs faster and allocates less memory.
My question is why does it happen? Does it mean that it is better to use the boxing approach in cases like this? Or did I write the performance test incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为装箱版本不需要将
i
转换为独立字符串作为中间步骤。相反,它使用装箱的i
直接构造结果字符串,这样总开销较小,因为它节省了一些内存分配。Because the boxing version does not need to convert
i
to a standalone string as an intermediate step. Rather it directly constructs the result string using the boxedi
, which has less total overhead because it saves some memory allocation.