• C# string 加号与StringBuilder .netCore3.1与 .netCore5.0速度测试


    using System;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                var startTime = DateTime.Now;           
                string str = "";
                for (int i = 0; i < 100; i++)
                {
                    str += i.ToString();
                }
    
                var ret = str;
                ret.Remove(0, 1);
                string userTime = (DateTime.Now - startTime).ToString();
                Console.WriteLine("         +:"+userTime);
                
                startTime = DateTime.Now;           
                StringBuilder str1=new StringBuilder();
                for (int i = 0; i < 100; i++)
                {
                    str1.Append(i.ToString());
                }
    
                var ret1 = str1.ToString();
                ret1.Remove(0, 1);
                userTime = (DateTime.Now - startTime).ToString();
                Console.WriteLine("    Append:"+userTime);
                
                
                startTime = DateTime.Now;           
                string str2 = "";
                str2 ="1212312312" +
                      "312dsakjf;ak" +
                      "sldjfaisdfjiad" +
                      "sfujadspfupfffff" +
                      "ffpfpfpfpfpfpfpfpfp" +
                      "fpfpfpfpfpfpfpfpfpfpf" +
                      "pfpfpfpfpfpfpfpfpfpfpfp" +
                      "fpfpfpfpfpfpfpfpfpfp";
                var ret2 = str2.ToString();
                ret2.Remove(0, 1);
                userTime = (DateTime.Now - startTime).ToString();
                Console.WriteLine("      Fix+:"+userTime);
                
                startTime = DateTime.Now;           
                StringBuilder str3 = new StringBuilder();
                str3.Append("1212312312");
                str3.Append("312dsakjf;ak" );
                str3.Append("sldjfaisdfjiad" );
                str3.Append("sfujadspfupfffff" );
                str3.Append("ffpfpfpfpfpfpfpfpfp" );
                str3.Append("fpfpfpfpfpfpfpfpfpfpf" );
                str3.Append("pfpfpfpfpfpfpfpfpfpfpfp" );
                str3.Append("fpfpfpfpfpfpfpfpfpfp");
                var ret3 = str3.ToString();
                ret3.Remove(0, 1);
                userTime = (DateTime.Now - startTime).ToString();
                Console.WriteLine("FixBuilder:"+userTime);
                Console.ReadKey();
                
            }
        }
    }

    测试结果取多次运行后的稳定值
    1、未开启优化:

    2、开启优化后:

     感觉性能相差不大,可能这种基本的类库从远古时代2.0之后一般不怎么改动。



  • 相关阅读:
    cenos安装memcache
    微信开发——测试号申请,接口配置,JS接口安全域名,自定义菜单
    mysql设计-优化
    mysql设计-基本操作
    CI框架部署后访问出现404
    ueditor的bug
    git操作
    github基本操作
    基于SSH协议clone GitHub远端仓库到本地-git
    Thinkphp5.0 路由
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/13895603.html
Copyright © 2020-2023  润新知