C# 中的字符串内插 $对比string.Format

2023-10-23 19:19:56 浏览数 (2)

原文:https://blog.csdn.net/HeBizhi1997/article/details/123544524

  C# 10.0 对字符串插值做了点提升,支持开发人员对字符串进行花式内插。   附官方教程:   https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/string-interpolation#code-try-0icon-default.png?t=M276https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/string-interpolation#code-try-0

  对比一下string.Format的方式,我想看下层层包装之后,性能上的差别。   先说下我机器的配置:

代码语言:javascript复制
#region 测试代码
var a = 3;
var b = 2;
var list = new List<int>();
Console.WriteLine("strat method one");
for (int j = 0; j < 10; j  )
{
    var dt1 = DateTime.Now;
    //Console.WriteLine(dt1.ToString("HH:mm ffff"));
    for (int i = 0; i < 1_0000_0000; i  )
    {
        var c = $"{a}   {b} = {a   b}";
    }
    var dt2 = DateTime.Now;
    //Console.WriteLine(dt2.ToString("HH:mm ffff"));
    Console.WriteLine("times "   j   " : "   (dt2 - dt1).Milliseconds);
    list.Add((dt2 - dt1).Milliseconds);
}
list.Remove(list.Max());
list.Remove(list.Min());
Console.WriteLine(list.Average());
 
list.Clear();
Console.WriteLine("strat method two");
for (int j = 0; j < 10; j  )
{
    var dt1 = DateTime.Now;
    //Console.WriteLine(dt1.ToString("HH:mm ffff"));
    for (int i = 0; i < 1_0000_0000; i  )
    {
        var c = string.Format("{0}   {1} = {2}", a, b, a   b);
    }
    var dt2 = DateTime.Now;
    //Console.WriteLine(dt2.ToString("HH:mm ffff"));
    Console.WriteLine("times "   j   " : "   (dt2 - dt1).Milliseconds);
    list.Add((dt2 - dt1).Milliseconds);
}
list.Remove(list.Max());
list.Remove(list.Min());
Console.WriteLine(list.Average()); 
#endregion

  输出结果:

   结论:从测试结果来说,string.Format占用的cpu资源更稳定,$语法糖就有些飘忽不定。

  但是无论是从功能扩展还是长期性能考虑来看的话,只能说微软牛逼,语法糖真香。

  附录:之前的测试代码反编译一下的结果

代码语言:javascript复制
int a = 3;
int b = 2;
List<int> list = new List<int>();
Console.WriteLine("strat method one");
for (int i = 0; i < 10; i  )
{
    DateTime dt = DateTime.Now;
    for (int j = 0; j < 100000000; j  )
    {
        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(6, 3);
        defaultInterpolatedStringHandler.AppendFormatted<int>(a);
        defaultInterpolatedStringHandler.AppendLiteral("   ");
        defaultInterpolatedStringHandler.AppendFormatted<int>(b);
        defaultInterpolatedStringHandler.AppendLiteral(" = ");
        defaultInterpolatedStringHandler.AppendFormatted<int>(a   b);
        string c = defaultInterpolatedStringHandler.ToStringAndClear();
    }
    DateTime dt2 = DateTime.Now;
    Console.WriteLine("times "   i.ToString()   " : "   (dt2 - dt).Milliseconds.ToString());
    list.Add((dt2 - dt).Milliseconds);
}
list.Remove(list.Max());
list.Remove(list.Min());
Console.WriteLine(list.Average());
list.Clear();
Console.WriteLine("strat method two");
for (int k = 0; k < 10; k  )
{
    DateTime dt3 = DateTime.Now;
    for (int l = 0; l < 100000000; l  )
    {
        string c2 = string.Format("{0}   {1} = {2}", a, b, a   b);
    }
    DateTime dt4 = DateTime.Now;
    Console.WriteLine("times "   k.ToString()   " : "   (dt4 - dt3).Milliseconds.ToString());
    list.Add((dt4 - dt3).Milliseconds);
}
list.Remove(list.Max());
list.Remove(list.Min());
Console.WriteLine(list.Average());

0 人点赞