Unity中的string gc优化
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/13928469.html
在项目中如果有大量的字符串拼接,比如每秒执行的倒计时,协议中的日志输出,每次拼接会产生大量的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的+=
对于参数的传递,可以传入引用类型,就不需要每次都构建一个新的对象
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
2013-11-04 NGUI类关系图
2013-11-04 u3d 模型ID配置