这段代码如何进一步优化?

发布于 2024-09-07 00:48:56 字数 715 浏览 6 评论 0原文

我刚刚收到了一个代码。该代码是用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-09-14 00:48:56

一个明显的问题:当你只用整数更新它们时,为什么你有一个二维小数数组?你能用 int[][] 来代替吗?

接下来,为什么要在每次迭代中访问 (int)table1Enum.barDateTime ?鉴于其中涉及转换,您可能会发现如果将其从循环中提取出来会有所帮助。

但是,我怀疑大部分时间都花在 _table1Values[i].Cast().ToArray() 上。你真的需要这样做吗?获取decimal[](或int[])的副本比在每次调用的每次迭代中装箱每个值 - 然后创建另一个数组要快。

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 the decimal[] (or int[]) would be faster than boxing every value on every iteration on every call - and then creating another array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文