c# 中的性能优化循环字符串连接
Performance optimizing looped string concatenations in c#
我有一个处理一些历史数据的应用程序。
我做了一些性能分析,并将以下功能确定为新的瓶颈:
public string GenerateSaveStringOptimized()
{
StringBuilder saveString = new StringBuilder();
saveString.Append( $"{this.TimeOfDay},{this.DayOfMonth},{this.DayOfYear},");
foreach (float value in this.MovingAverage3h) saveString.Append($"{value},");
foreach (float value in this.MovingAverage6h) saveString.Append($"{value},");
foreach (float value in this.MovingAverage1d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage6d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage12d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage24d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage48d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage96d) saveString.Append($"{value},");
foreach (float value in this.RSI3h) saveString.Append($"{value},");
foreach (float value in this.RSI6h) saveString.Append($"{value},");
foreach (float value in this.RSI1d) saveString.Append($"{value},");
foreach (float value in this.RSI6d) saveString.Append($"{value},");
foreach (float value in this.RSI12d) saveString.Append($"{value},");
foreach (float value in this.Momentum1h) saveString.Append($"{value},");
foreach (float value in this.SpotPrices1h) saveString.Append($"{value},");
foreach (float value in this.BuyPrices1h) saveString.Append($"{value},");
foreach (float value in this.SellPrices1h) saveString.Append($"{value},");
saveString.Append($"{this.Label}");
return saveString.ToString();
}
结果将是一个 csv 格式的字符串,例如:
0,1,1,0.4916667,0.4916667,0.4916667,0.4916667,0.4916667,0.49,0.49,0.49,0.49,0.4897351,
[...]
每个数组都包含 25 个浮点值,因此代码类似于以下伪代码:
for (int i = 0; i < 17; i++)
{
for (b = 0; b < 25 b++)
{
saveString.Append("Value,")
}
}
每次调用该函数都会产生 17 * 25 = 425 个字符串连接!
有什么办法可以优化吗?
Top performance eater 似乎正在“等待 cpu”和“AppendformatHelper”,它是 StringBuilder 的内置方法。不幸的是,我不知道如何轻松处理
也许使用如下硬编码设置?
saveString.Append($"{MA3h[0]},{MA3h[1]},{MA3h[2]},...");
saveString.Append($"{MA6h[0]},{MA6h[1]},{MA6h[2]},...");
saveString.Append($"{MA1d[0]},{MA1d[1]},{MA1d[2]},...");
改用以下循环取得了巨大的成功:
foreach (float value in this.RSI1d)
{
saveString.Append(value.ToString());
saveString.Append(',');
}
非常感谢 user1672994、Guru Stron 和 Theodor Zoulias
评论中提到的其他内容也会尝试。
预分配足够的容量。
StringBuilder saveString = new StringBuilder(100000);
不要使用格式或插值。
saveString.Append(this.TimeOfDay).Append(',').Append(this.DayOfMonth).Append(',').Append(this.DayOfYear).Append(',');
在所有循环中执行类似的替换。
foreach (float value in this.MovingAverage3h) saveString.Append(value).Append(',');
不要使用格式或插值。
saveString.Append(this.Label);
Append
方法有一个接受float
的重载,所以不会有装箱。
我有一个处理一些历史数据的应用程序。 我做了一些性能分析,并将以下功能确定为新的瓶颈:
public string GenerateSaveStringOptimized()
{
StringBuilder saveString = new StringBuilder();
saveString.Append( $"{this.TimeOfDay},{this.DayOfMonth},{this.DayOfYear},");
foreach (float value in this.MovingAverage3h) saveString.Append($"{value},");
foreach (float value in this.MovingAverage6h) saveString.Append($"{value},");
foreach (float value in this.MovingAverage1d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage6d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage12d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage24d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage48d) saveString.Append($"{value},");
foreach (float value in this.MovingAverage96d) saveString.Append($"{value},");
foreach (float value in this.RSI3h) saveString.Append($"{value},");
foreach (float value in this.RSI6h) saveString.Append($"{value},");
foreach (float value in this.RSI1d) saveString.Append($"{value},");
foreach (float value in this.RSI6d) saveString.Append($"{value},");
foreach (float value in this.RSI12d) saveString.Append($"{value},");
foreach (float value in this.Momentum1h) saveString.Append($"{value},");
foreach (float value in this.SpotPrices1h) saveString.Append($"{value},");
foreach (float value in this.BuyPrices1h) saveString.Append($"{value},");
foreach (float value in this.SellPrices1h) saveString.Append($"{value},");
saveString.Append($"{this.Label}");
return saveString.ToString();
}
结果将是一个 csv 格式的字符串,例如:
0,1,1,0.4916667,0.4916667,0.4916667,0.4916667,0.4916667,0.49,0.49,0.49,0.49,0.4897351, [...]
每个数组都包含 25 个浮点值,因此代码类似于以下伪代码:
for (int i = 0; i < 17; i++)
{
for (b = 0; b < 25 b++)
{
saveString.Append("Value,")
}
}
每次调用该函数都会产生 17 * 25 = 425 个字符串连接!
有什么办法可以优化吗?
Top performance eater 似乎正在“等待 cpu”和“AppendformatHelper”,它是 StringBuilder 的内置方法。不幸的是,我不知道如何轻松处理
也许使用如下硬编码设置?
saveString.Append($"{MA3h[0]},{MA3h[1]},{MA3h[2]},...");
saveString.Append($"{MA6h[0]},{MA6h[1]},{MA6h[2]},...");
saveString.Append($"{MA1d[0]},{MA1d[1]},{MA1d[2]},...");
改用以下循环取得了巨大的成功:
foreach (float value in this.RSI1d)
{
saveString.Append(value.ToString());
saveString.Append(',');
}
非常感谢 user1672994、Guru Stron 和 Theodor Zoulias
评论中提到的其他内容也会尝试。
预分配足够的容量。
StringBuilder saveString = new StringBuilder(100000);
不要使用格式或插值。
saveString.Append(this.TimeOfDay).Append(',').Append(this.DayOfMonth).Append(',').Append(this.DayOfYear).Append(',');
在所有循环中执行类似的替换。
foreach (float value in this.MovingAverage3h) saveString.Append(value).Append(',');
不要使用格式或插值。
saveString.Append(this.Label);
Append
方法有一个接受float
的重载,所以不会有装箱。