| | |
| | | using UnityEngine; |
| | | using UnityEngine; |
| | | using System.Collections; |
| | | using System.Text; |
| | | using System; |
| | | |
| | | public class StringUtility |
| | | { |
| | | public static string[] splitSeparator = new string[] { "|" }; |
| | | public static readonly string[] splitSeparator = new string[] { "|" }; |
| | | |
| | | private static StringBuilder s_CacheStringBuilder = new StringBuilder(); |
| | | |
| | | static object lockObject = new object(); |
| | | |
| | | public static string Contact(params object[] _objects) |
| | | // 新增:为循环调用的优化版本(复用StringBuilder) |
| | | [ThreadStatic] |
| | | private static StringBuilder _threadLocalStringBuilder; |
| | | |
| | | /// <summary> |
| | | /// 获取当前线程的StringBuilder |
| | | /// </summary> |
| | | private static StringBuilder GetThreadLocalStringBuilder() |
| | | { |
| | | lock (lockObject) |
| | | if (_threadLocalStringBuilder == null) |
| | | { |
| | | s_CacheStringBuilder.Remove(0, s_CacheStringBuilder.Length); |
| | | for (int i = 0; i < _objects.Length; ++i) |
| | | { |
| | | if (_objects[i] != null) |
| | | { |
| | | s_CacheStringBuilder.Append(_objects[i]); |
| | | } |
| | | } |
| | | return s_CacheStringBuilder.ToString(); |
| | | _threadLocalStringBuilder = new StringBuilder(); |
| | | } |
| | | return _threadLocalStringBuilder; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 智能字符串拼接方法,自动选择最优策略 |
| | | /// - 2-4个字符串:直接使用 string.Concat() (最高性能) |
| | | /// - 5个以上字符串:使用线程本地 StringBuilder 复用 (循环优化) |
| | | /// </summary> |
| | | public static string Concat(params string[] _objects) |
| | | { |
| | | // 少量字符串直接使用Concat,性能最佳 |
| | | if (_objects.Length <= 4) |
| | | { |
| | | return string.Concat(_objects); |
| | | } |
| | | |
| | | // 大量字符串使用线程本地StringBuilder,避免重复创建 |
| | | var sb = GetThreadLocalStringBuilder(); |
| | | sb.Clear(); |
| | | foreach (string str in _objects) |
| | | { |
| | | sb.Append(str); |
| | | } |
| | | return sb.ToString(); |
| | | } |
| | | |
| | | // AI提醒实际在现代编辑器中,低于4个字符串的+ 操作符的性能和 string.Concat 几乎相同,会被智能优化为 string.Concat |
| | | // 添加常用2个字符串拼接的优化版本 |
| | | public static string Contact(string str1, string str2) |
| | | { |
| | | return string.Concat(str1, str2); |
| | | } |
| | | |
| | | // 添加常用3个字符串拼接的优化版本 |
| | | public static string Contact(string str1, string str2, string str3) |
| | | { |
| | | return string.Concat(str1, str2, str3); |
| | | } |
| | | |
| | | // 添加常用4个字符串拼接的优化版本 |
| | | public static string Contact(string str1, string str2, string str3, string str4) |
| | | { |
| | | return string.Concat(str1, str2, str3, str4); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static string FormatSpeed(float speed) |
| | | { |
| | | if (speed > 1048576f) |
| | | { |
| | | return StringUtility.Contact((speed / 1048576f).ToString("f1"), " M/S"); |
| | | return Contact((speed / 1048576f).ToString("f1"), " M/S"); |
| | | } |
| | | else if (speed > 1024f) |
| | | { |
| | | return StringUtility.Contact((speed / 1024f).ToString("f1"), " KB/S"); |
| | | return Contact((speed / 1024f).ToString("f1"), " KB/S"); |
| | | } |
| | | else |
| | | { |
| | | return StringUtility.Contact(speed.ToString("f1"), " B/S"); |
| | | }; |
| | | return Contact(speed.ToString("f1"), " B/S"); |
| | | } |
| | | } |
| | | |
| | | } |