string.format()是否應該多用?
前一陣子,項目中的一個頁面每秒只能處理300次,而這個頁面的邏輯也不復雜,就是根據條件拼出一個字串然後輸出。開始以爲這裏面邏輯太複雜,所以有問題。不過後面發現了vs裏面帶了性能分析工具,於是抱着試試看的想法,作了一下性能分析。最後的結果讓人大吃一驚:string.format這個操作竟然用掉了一半的時間,爲啥它會這麼費時間呢?爲了真相,我用.net reflector查看了string的實現:
public static string Format(IFormatProvider provider, string format, params object[] args)
{
if ((format == null) || (args == null))
{
throw new ArgumentNullException((format == null) ? "format" : "args");
}
StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
builder.AppendFormat(provider, format, args);
return builder.ToString();
}
很讓人吃驚,string.format竟然是調用了StringBuilder的AppendFormat來實現的。再繼續根下去(這個源碼只是通過IL得來的,可能和原始的不太一樣,但是差不多了),注意裏面的紅色的那句:
public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args)
{
int num3;
if ((format == null) || (args == null))
{
throw new ArgumentNullException((format == null) ? "format" : "args");
}
char[] chArray = format.ToCharArray(0, format.Length);
int index = 0;
int length = chArray.Length;
char ch = '/0';
ICustomFormatter formatter = null;
if (provider != null)
{
formatter = (ICustomFormatter) provider.GetFormat(typeof(ICustomFormatter));
}
Label_004E:
num3 = index;
int num4 = index;
while (index < length)
{
ch = chArray[index];
index++;
if (ch == '}')
{
if ((index < length) && (chArray[index] == '}'))
{
index++;
}
else
{
FormatError();
}
}
if (ch == '{')
{
if ((index < length) && (chArray[index] == '{'))
{
index++;
}
else
{
index--;
break;
}
}
chArray[num4++] = ch;
}
if (num4 > num3)
{
this.Append(chArray, num3, num4 - num3);
}
if (index == length)
{
return this;
}
index++;
if (((index == length) || ((ch = chArray[index]) < '0')) || (ch > '9'))
{
FormatError();
}
int num5 = 0;
do
{
num5 = ((num5 * 10) + ch) - 0x30;
index++;
if (index == length)
{
FormatError();
}
ch = chArray[index];
}
while (((ch >= '0') && (ch <= '9')) && (num5 < 0xf4240));
if (num5 >= args.Length)
{
throw new FormatException(Environment.GetResourceString("Format_IndexOutOfRange"));
}
while ((index < length) && ((ch = chArray[index]) == ' '))
{
index++;
}
bool flag = false;
int num6 = 0;
if (ch == ',')
{
index++;
while ((index < length) && (chArray[index] == ' '))
{
index++;
}
if (index == length)
{
FormatError();
}
ch = chArray[index];
if (ch == '-')
{
flag = true;
index++;
if (index == length)
{
FormatError();
}
ch = chArray[index];
}
if ((ch < '0') || (ch > '9'))
{
FormatError();
}
do
{
num6 = ((num6 * 10) + ch) - 0x30;
index++;
if (index == length)
{
FormatError();
}
ch = chArray[index];
}
while (((ch >= '0') && (ch <= '9')) && (num6 < 0xf4240));
}
while ((index < length) && ((ch = chArray[index]) == ' '))
{
index++;
}
object arg = args[num5];
string str = null;
if (ch == ':')
{
index++;
num3 = index;
num4 = index;
while (true)
{
if (index == length)
{
FormatError();
}
ch = chArray[index];
index++;
switch (ch)
{
case '{':
if ((index < length) && (chArray[index] == '{'))
{
index++;
}
else
{
FormatError();
}
break;
case '}':
if ((index < length) && (chArray[index] == '}'))
{
index++;
}
else
{
index--;
if (num4 > num3)
{
str = new string(chArray, num3, num4 - num3);
}
goto Label_0253;
}
break;
}
chArray[num4++] = ch;
}
}
Label_0253:
if (ch != '}')
{
FormatError();
}
index++;
string str2 = null;
if (formatter != null)
{
str2 = formatter.Format(str, arg, provider);
}
if (str2 == null)
{
if (arg is IFormattable)
{
str2 = ((IFormattable) arg).ToString(str, provider);
}
else if (arg != null)
{
str2 = arg.ToString();
}
}
if (str2 == null)
{
str2 = string.Empty;
}
int repeatCount = num6 - str2.Length;
if (!flag && (repeatCount > 0))
{
this.Append(' ', repeatCount);
}
this.Append(str2);
if (flag && (repeatCount > 0))
{
this.Append(' ', repeatCount);
}
goto Label_004E;
}
發現裏面會有new string,這時候會有新的內存分配出現,也就是說string.format會產生很多臨時的string對象,這個會費時間,同時也會使GC的工作量增加.既然這裏面調用了stringbuilder來實現的,那爲啥不直接調用stringbuilder.append來實現。於是我就把原來的實現改成了stringbuilder的append,同時設置它初始容量爲我們預期的大小,通過測試,這部分的性能提高了十倍。於是性能問題解決了。
最後,我覺得如果程序的性能很重要,而在這裏面又經常有string.format的時候,還是改用stringbuilder.append來實現,雖然麻煩一些,代碼也不好看,但是效果還是會很明顯的。