在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
通过unity profiler测试的代码,及运行结果 using UnityEngine; using UnityEngine.Profiling; using System.Text; #if UNITY_5_5_OR_NEWER using TProfiler = UnityEngine.Profiling.Profiler; #else using TProfiler = UnityEngine.Profiler; #endif public namespace Test { /// string concat test public class TestString : MonoBehaviour { // Update is called once per frame void Update () { // loop 10000times for(int i = 0; i < 10000; ++i) { TestString(); } TestString10000(); } // object new once private StringBuilder _sb = new StringBuilder(); /// concat 10000 times private void TestString10000() { // + TProfiler.BeginSample("string.(10000+)"); string s1 = string.Empty; for(int i = 0; i < 10000; ++i) { s1 = s1 + ((int)Time.time).ToString(); } TProfiler.EndSample(); // StringBuilder Append TProfiler.BeginSample("StringBuilder.10000Append"); _sb.Clear(); for(int i = 0; i < 10000; ++i) { _sb.Append((int)Time.time); } string s2 = _sb.ToString(); TProfiler.EndSample(); } /// concat 3 parts private void TestString() { System.Random random = new System.Random(); // + TProfiler.BeginSample("string.(+)"); string s1 = "txt_pre_" + random.Next().ToString() + "_end"; TProfiler.EndSample(); // Concat TProfiler.BeginSample("string.Concat"); string s2 = string.Concat("txt_pre_", random.Next(), "_end"); TProfiler.EndSample(); // Concat ToString() TProfiler.BeginSample("string.Concat & int.ToString"); string s3 = string.Concat("txt_pre_", ((int)Time.time).ToString(), "_end"); TProfiler.EndSample(); // format TProfiler.BeginSample("string.Format"); string s4 = string.Format("txt_pre_{0}_end", (int)Time.time); TProfiler.EndSample(); // format ToString() TProfiler.BeginSample("string.Format & int.ToString"); string s5 = string.Format("txt_pre_{0}_end", ((int)Time.time).ToString()); TProfiler.EndSample(); // StringBuilder AppendFormat TProfiler.BeginSample("StringBuilder.AppendFormat & int.ToString"); _sb.Clear(); _sb.AppendFormat("txt_pre_{0}_end", ((int)Time.time).ToString()); string s6 = _sb.ToString(); TProfiler.EndSample(); // StringBuilder Append TProfiler.BeginSample("StringBuilder.Append"); _sb.Clear(); _sb.Append("txt_pre_"); _sb.Append((int)Time.time); _sb.Append("_end"); string s7 = _sb.ToString(); TProfiler.EndSample(); // StringBuilder Append ToString() TProfiler.BeginSample("StringBuilder.Append & int.ToString"); _sb.Clear(); _sb.Append("txt_pre_"); _sb.Append(((int)Time.time).ToString()); _sb.Append("_end"); string s8 = _sb.ToString(); TProfiler.EndSample(); // StringBuilder AppendNumber TProfiler.BeginSample("StringBuilder.AppendNumber"); _sb.Clear(); _sb.Append("txt_pre_"); _sb.AppendNumber((int)Time.time); _sb.Append("_end"); string s9 = _sb.ToString(); TProfiler.EndSample(); } } } 运行结果:
结果分析及结论:
可读性好
可读性一般
可读性较好
一个通用的int(Enum)转string的缓存方案 /// Sample for SimpleDictCache /// private static SimpleDictCache<EnumType> nameCache; /// 指挥官头像icon // public static string GetName(EnumType id) // { // if (nameCache == null) // nameCache = new SimpleDictCache<EnumType>(); // // return nameCache.Get((int)id, id, (o) => { // return o.ToString(); // 此处可以替换为复杂的获取string的实现 // }); // } /// 通用Dictionary<int, string>缓存 /// 比如enum对应的string可以用cache方式避免反复ToString()操作 class SimpleDictCache<T> { public delegate string CacheValue(T id); private Dictionary<int, string> data; public string Get(int k, T id, CacheValue cv) { if (data == null) data = new Dictionary<int, string>(); if (data.ContainsKey(k)) { return data[k]; } if (cv != null) data.Add(k, cv(id)); else data.Add(k, string.Empty); return data[k]; } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论