这段代码如何进一步优化?
我刚刚收到了一个代码。该代码是用 C# 编写的,每秒将实时数据插入数据库。数据随着时间的推移而积累,使得数字变得很大。
数据在一秒内更新多次,然后在第二个结果结束时获取并插入。
我们过去常常通过属性直接在秒内寻址数据集行。例如,许多像这样的操作 'datavaluerow.meanvalue += Mean;可能会发生。 我们发现,由于内部转换完成,运行探查器后这会降低性能,因此我们创建了二维小数数组,在其上进行更新,然后仅在第二秒末尾将值分配给数据行。 我运行了一个探查器,发现它仍然花费大量时间(尽管加起来少于频繁访问数据行所花费的时间)。
第二个结束时执行的代码如下
public void UpdateDataRows(int tick)
{
//ord
//_table1Values is of type decimal[][]
for (int i = 0; i < _table1Values.Length; i++)
{
_table1Values[i][(int)table1Enum.barDateTime] = tick;
table1Row[i].ItemArray = _table1Values[i].Cast<object>().ToArray();
}
// this process is done for other 10 tables
}
有没有办法进一步改进这种方法。
I just got a code handed over to me. The code is written in C# and it inserts realtime data into database every second. The data is accumulated in time which makes the numbers big.
The data is updated within the second many times then at the end of the second result is taken and inserted.
We used to address the dataset rows directly within the second through the properties. For example many operations like this one 'datavaluerow.meanvalue += mean; could take place.
we figured out that this is degrading the performance after running the profiler becuase of the internal casting done so we created 2d array of decimals on which the updates are carried out then the values are assigned to the datarows only at the end of the second.
I ran a profiler and found out that it is still taking a lot of time (although less than the time spent accessing datarows frequently when added up).
The code that is exectued at the end of the second is as follows
public void UpdateDataRows(int tick)
{
//ord
//_table1Values is of type decimal[][]
for (int i = 0; i < _table1Values.Length; i++)
{
_table1Values[i][(int)table1Enum.barDateTime] = tick;
table1Row[i].ItemArray = _table1Values[i].Cast<object>().ToArray();
}
// this process is done for other 10 tables
}
Is there a way to further improve this approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个明显的问题:当你只用整数更新它们时,为什么你有一个二维小数数组?你能用
int[][]
来代替吗?接下来,为什么要在每次迭代中访问
(int)table1Enum.barDateTime
?鉴于其中涉及转换,您可能会发现如果将其从循环中提取出来会有所帮助。但是,我怀疑大部分时间都花在
_table1Values[i].Cast
One obvious question: why do you have a 2D array of decimals when you're only updating them with integers? Could you get away with an
int[][]
instead?Next, why are you accessing
(int)table1Enum.barDateTime
on each iteration? Given that there's a conversion involved there, you may find it helps if you extract that out of the loop.However, I suspect the majority of the time is going to be spent in
_table1Values[i].Cast<object>().ToArray()
. Do you really need to do that? Taking a copy of thedecimal[]
(orint[]
) would be faster than boxing every value on every iteration on every call - and then creating another array.