string 性能测试

API地址

定义

将文本表示为 UTF-16 代码单元的序列。

注解

字符串是用于表示文本的字符的有序集合。 String对象是对象的有序集合 System.Char ,表示字符串; System.Char 对象对应于 utf-16 代码单元。 对象的值 String 是对象的顺序集合的内容 System.Char ,并且该值是不可变的 (也就是说,它是只读)

测试

1、修改值

因为是只读的,那么如果对它进行赋值的话其实是给它分配了另外的内存地址
测试实例:

public string GetMemory(System.Object o)
 {
     GCHandle h = GCHandle.Alloc(o, GCHandleType.Pinned);
     IntPtr addr = h.AddrOfPinnedObject();
     return "0x" + addr.ToString("X");
 }
 
string s = "aa";
 Debug.LogError(GetMemory(s));
  Debug.LogError("---------------");
  s = "bb";
  Debug.LogError(GetMemory(s));
  Debug.LogError("---------------");

输出结果:
在这里插入图片描述

2、执行字符串拼接消耗

void testStringConnect(int num) {
     string str = "";
     for (int i = 0; i < num; i++) {
         str += i.ToString();
     }
 }
 void testStringBuilder(int num) {
     System.Text.StringBuilder build = new System.Text.StringBuilder();
     for (int i = 0; i < num; i++)
     {
         build.Append(i.ToString());
     }
 }
int start = Environment.TickCount;
testStringConnect(10000);
int connect = Environment.TickCount;
testStringBuilder(10000);
int endTime = Environment.TickCount;
Debug.LogErrorFormat("运行 10000 次耗时 connectCostTime:{0},buildCostTime:{1}", connect-start,endTime-connect);

执行消耗:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值