三国卡牌客户端基础资源仓库
hch
9 天以前 d1d7a6dcbd4bd0d689d0d379d2d887d01b0beacb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
using System.Collections;
using System.Text;
using System;
 
public class StringUtility
{
    public static readonly string[] splitSeparator = new string[] { "|" };
 
 
    // 新增:为循环调用的优化版本(复用StringBuilder)
    [ThreadStatic]
    private static StringBuilder _threadLocalStringBuilder;
    
    /// <summary>
    /// 获取当前线程的StringBuilder
    /// </summary>
    private static StringBuilder GetThreadLocalStringBuilder()
    {
        if (_threadLocalStringBuilder == null)
        {
            _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 Contact((speed / 1048576f).ToString("f1"), " M/S");
        }
        else if (speed > 1024f)
        {
            return Contact((speed / 1024f).ToString("f1"), " KB/S");
        }
        else
        {
            return Contact(speed.ToString("f1"), " B/S");
        }
    }
 
}