现在的项目状况 拿到的数据源为文本形式(xml文件) 数据必须要实时计算,所以都有通过 .Parse来进行操作
其中 每笔数据 包括 15 次 float.Parse (数据精度为 9,3)和 12 次 decimal.Parse(数据精度为 18,0 精确到个位) (不可以缩减) 数据更新频率为5s/次,2000笔数据
代码的执行效率十分糟糕,CPU占用80%以上(Intel E5300 2.6G),消耗时间20s以上有没有替代方式?或优化方案?
其中 每笔数据 包括 15 次 float.Parse (数据精度为 9,3)和 12 次 decimal.Parse(数据精度为 18,0 精确到个位) (不可以缩减) 数据更新频率为5s/次,2000笔数据
代码的执行效率十分糟糕,CPU占用80%以上(Intel E5300 2.6G),消耗时间20s以上有没有替代方式?或优化方案?
可能是 I/O 方面做得不好
简单就是定义两个datetime到时候再相减!
不过SQL有执行计划,能够测试一下哪种效率会更好点
没注销是 80~90%,18~25秒CompleteData CompleteData = new CompleteData
{
Code = stock["Code"],
Market = Settings.Market,
Name = stock["Name"],
Time = time, RePrice = Functions.ToFloat(stock["RePrice"]),
Open = Functions.ToFloat(stock["Open"]),
High = Functions.ToFloat(stock["High"]),
Low = Functions.ToFloat(stock["Low"]),
Price = Functions.ToFloat(stock["Price"]), Amount = Functions.ToDecimal(stock["Amount"]),
Volume = Functions.ToDecimal(stock["Volume"]), BuyPrice = new float[]
{
Functions.ToFloat(stock["BuyP0"]),
Functions.ToFloat(stock["BuyP1"]),
Functions.ToFloat(stock["BuyP2"]),
Functions.ToFloat(stock["BuyP3"]),
Functions.ToFloat(stock["BuyP4"]),
},
BuyVolume = new decimal[]
{
Functions.ToDecimal(stock["BuyV0"]),
Functions.ToDecimal(stock["BuyV1"]),
Functions.ToDecimal(stock["BuyV2"]),
Functions.ToDecimal(stock["BuyV3"]),
Functions.ToDecimal(stock["BuyV4"]),
},
SellPrice = new float[]
{
Functions.ToFloat(stock["SellP0"]),
Functions.ToFloat(stock["SellP1"]),
Functions.ToFloat(stock["SellP2"]),
Functions.ToFloat(stock["SellP3"]),
Functions.ToFloat(stock["SellP4"]),
},
SellVolume = new decimal[]
{
Functions.ToDecimal(stock["SellV0"]),
Functions.ToDecimal(stock["SellV1"]),
Functions.ToDecimal(stock["SellV2"]),
Functions.ToDecimal(stock["SellV3"]),
Functions.ToDecimal(stock["SellV4"]),
}
}; class Functions
{
public static float ToFloat(string value)
{
try
{ return float.Parse(value); }
catch { return 0; }
} public static decimal ToDecimal(string value)
{
try
{
return decimal.Parse(value);
}
catch
{
return 0;
}
}
}
还真是这个的问题,我把try-catch去掉旧正常了。
可是没异常处理怎么办啊,我不想忽略一条数据阿~
stock[string key]
{
get 这里面代码怎么写的,估计是这里面慢了
}
float.TryParse();可以去掉try catch了
只是说可能,再说没定论前,你也不要这么肯定自己的观点,现在的CPU处理Parse还能撑不住?
CPU 密集型
I/O 密集型
两类的特征和优化策略好多书都有讲的吧
在楼主没贴代码前,我相信大多数实时系统的瓶颈在 IO。
目前代码看,既不是IO 密集型,也不是CPU密集型,可能try 用点滥用过度
以及stock[string key]的代码,来做下定论
float.TryParse();
判断下就可以TryParse()返回类型BOOL是true return 值。false return 0
stock就是一个 Dictionary<string, string> 型字典,初步切割后的结果
TryParse 还不会用,翻手册中
还真是 try-carch 滥用造成的
。
现在的CPU 3~5%,处理时间800毫秒以下
void SetValue(DbField field ,TEntity entity,object value,TEnttiy @default)
try
{
return field.propertyInfo.SetValue(entity, field.ConvertFromDb(value), null);
}
catch
{
return field.propertyInfo.SetValue(entity, field.ConvertFromDb(field.propertyInfo.GetValue(@default, null)), null);
}