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

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

posted @ 2020-11-04 20:42  赵青青  阅读(2534)  评论(0编辑  收藏  举报