zoukankan      html  css  js  c++  java
  • Unity中的string gc优化

    在项目中如果有大量的字符串拼接,比如每秒执行的倒计时,协议中的日志输出,每次拼接会产生大量的gc,尤其是在ILRuntime下执行 gc alloc的次数会更加频繁。

    zstring

    有两个字符串处理的库都叫zstring,其中小写的zstring是一款国人开源的zstring,而大写的ZString是日本的CySharp公司的

    国人的zstring:https://github.com/871041532/zstring

    ZString:https://github.com/Cysharp/ZString

    经测试zstring在update中是0gc,而ZString还有gc,短字符串处理,会比原生string慢一些,我的测试环境:Unity2019.3.4f1

    gc分配值:string >ZString >zstring

    执行耗时:zstring >ZString >string

    易用性:string>zstring>ZString

    测试代码

    void Update()
    {
    	Profiler.BeginSample("zstring=============concat");
    	ZString.Concat("abc", 1);
    	Profiler.EndSample();
    	
    	Profiler.BeginSample("string=============concat");
    	string.Concat("abc", 1);
    	Profiler.EndSample();
    	
    	Profiler.BeginSample("cn-zstring=============concat");
    	using (zstring.Block())
    	{
    		zstring.Concat("abc",1);
    	}
    	Profiler.EndSample();
    	
    	Profiler.BeginSample("zstring=============format");
    	ZString.Format("hello,{0}",1111);
    	Profiler.EndSample();
    	
    	Profiler.BeginSample("string=============format");
    	string.Format("hello,{0}",1111);
    	Profiler.EndSample();
    	
    	Profiler.BeginSample("cn-zstring=============format");
    	using (zstring.Block())
    	{
    		zstring.Format("hello ,{0}",1111);
    	}
    	Profiler.EndSample();
    }
    

    ZString和string gc对比

    Method Allocated(B) Mean(ns) Mean(ns)
    StringPlus 224 126.66 126.66
    ZStringConcat 56 96.95 96.95
    StringFormat 128 158.21 158.21
    ZStringFormat 56 185.36 185.36
    StringBuilder 296 144.2 144.2
    ZStringBuilder 56 131.68 131.68

    结论

    个人而言更倾向于使用国人开发的zstring,它只有一个代码文件,放Plugins目录下就可以使用,相对而言上手更容易

    Unity的建议

    关于内存管理可看这篇: https://docs.unity3d.com/cn/current/Manual/UnderstandingAutomaticMemoryManagement.html

    其中有条建议,对于需要频繁拼接的字符串,先判断字符串是否有变化,无变化则不需要拼接

    对于字符串的拼接,使用stringbuilder代替string,同时我也建议使用zstring代替string的+=

    对于参数的传递,可以传入引用类型,就不需要每次都构建一个新的对象

  • 相关阅读:
    axure rp8.0 序列号,亲测可以用
    纯html页面之间传参
    js下载项目中的文件
    Java获取用户ip
    阿里云部署多个tomcat
    少小有才国家用,老大空长做何为
    获取 web 服务器 port
    知识的迁移和学习能力才是最重要的
    eclipse 安装和使用AmaterasUML
    Eclipse中tomcat更改部署路径 deply path
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/13928469.html
Copyright © 2011-2022 走看看