• 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的+=

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

  • 相关阅读:
    【2017 Multi-University Training Contest
    【“玲珑杯”ACM比赛 Round #20 H】康娜的数学课
    【hdu 6181】Two Paths
    Cache coherence protocol
    C#实现图片的无损压缩
    收集一些常用的正则表达式
    收集一些常用的正则表达式
    收集一些常用的正则表达式
    Sql中存储过程的定义、修改和删除操作
    Sql中存储过程的定义、修改和删除操作
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/13928469.html
Copyright © 2020-2023  润新知