代码性能问题

发布于 2024-11-13 04:30:17 字数 1048 浏览 3 评论 0原文

假设我有一个相对较大的名为 MyBigList 的对象 MyObjectModel 列表。 MyObjectModel 的属性之一是一个名为 ObjectIDint。理论上,我认为MyBigList的大小可以达到15-20MB。我的数据库中还有一个表,用于存储有关此列表的一些标量,以便稍后可以对其进行重组。

什么会更有效率?

选项 A:

   List<MyObjectModel> MyBigList = null;

   MyBigList = GetBigList(some parameters);

   int RowID = PutScalarsInDB(MyBigList);

选项 B:

   List<MyObjectModel> MyBigList = null;

   MyBigList = GetBigList(some parameters);

   int TheCount = MyBigList.Count();

   StringBuilder ListOfObjectID = null;

   foreach (MyObjectModel ThisObject in MyBigList)
   {
      ListOfObjectID.Append(ThisObject.ObjectID.ToString());
   }

   int RowID = PutScalarsInDB ( TheCount, ListOfObjectID);

在选项 AI 中,将 MyBigList 传递给从列表中提取标量的函数,将它们存储在数据库中并返回创建这些条目的行。在选项 B 中,我将 MyBigList 保留在页面方法中,在其中提取标量,然后将它们传递给 PutScalarsInDB 函数。

什么是更好的选择?可能还有一个更好?我担心传递这个大小的对象和内存使用

Let's say I have a relatively large list of an object MyObjectModel called MyBigList. One of the properties of MyObjectModel is an int called ObjectID. In theory, I think MyBigList could reach 15-20MB in size. I also have a table in my database that stores some scalars about this list so that it can be recomposed later.

What is going to be more efficient?

Option A:

   List<MyObjectModel> MyBigList = null;

   MyBigList = GetBigList(some parameters);

   int RowID = PutScalarsInDB(MyBigList);

Option B:

   List<MyObjectModel> MyBigList = null;

   MyBigList = GetBigList(some parameters);

   int TheCount = MyBigList.Count();

   StringBuilder ListOfObjectID = null;

   foreach (MyObjectModel ThisObject in MyBigList)
   {
      ListOfObjectID.Append(ThisObject.ObjectID.ToString());
   }

   int RowID = PutScalarsInDB ( TheCount, ListOfObjectID);

In option A I pass MyBigList to a function that extracts the scalars from the list, stores these in the DB and returns the row where these entries were made. In option B, I keep MyBigList in the page method where I do the extraction of the scalars and I just pass these to the PutScalarsInDB function.

What's the better option, and it could be that yet another is better? I'm concerned about passing around objects this size and memory usage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

贱贱哒 2024-11-20 04:30:17

我认为您不会发现这两种方法之间存在实质性差异。根据您的描述,听起来无论哪种方式您都会消耗相同的 CPU 周期。重要的是:

  • 获取列表
  • 迭代列表以获取 ID
  • 迭代列表以更新数据库

这三个活动发生的顺序以及它们是否发生在单个方法或子例程中并不重要。所有其他活动(声明变量、分配结果等)对性能的影响为零甚至可以忽略不计。

在其他条件相同的情况下,您的第一个选项的性能可能会稍高一些,因为我认为您只会迭代一次,一次即可提取 ID 并更新数据库。但与更新数据库的成本相比,迭代的成本可能非常小,因此您可能不会注意到性能差异。

话虽如此,还有很多很多因素可能会影响性能,例如您正在迭代的列表的类型、连接到数据库的速度等,这些因素可能会使这些其他考虑因素相形见绌。无论哪种方式,它看起来都不像太多代码。我强烈建议构建并测试它们。

然后让我们知道您的结果!

I don't think you'll see a material difference between these two approaches. From your description, it sounds like you'll be burning the same CPU cycles either way. The things that matter are:

  • Get the list
  • Iterate through the list to get the IDs
  • Iterate through the list to update the database

The order in which these three activities occur, and whether they occur within a single method or a subroutine, doesn't matter. All other activities (declaring variables, assigning results, etc.,) are of zero to negligible performance impact.

Other things being equal, your first option may be slightly more performant because you'll only be iterating once, I assume, both extracting IDs and updating the database in a single pass. But the cost of iteration will likely be very small compared with the cost of updating the database, so it's not a performance difference you're likely to notice.

Having said all that, there are many, many more factors that may impact performance, such as the type of list you're iterating through, the speed of your connection to the database, etc., that could dwarf these other considerations. It doesn't look like too much code either way. I'd strongly suggesting building both and testing them.

Then let us know your results!

指尖微凉心微凉 2024-11-20 04:30:17

如果您想知道哪种方法具有更高的性能,您可以使用秒表类来检查每种方法所需的时间。请参阅此处了解秒表用法: http://www.dotnetperls.com/stopwatch

我认为还有其他问题对于 ASP.NET 应用程序,您需要验证:

  1. 从哪里读取您的列表?如果您从数据库中读取它,那么在存储过程中在数据库中完成工作会更有效吗?
  2. 它存储在哪里?它只是读取并销毁还是存储在会话或应用程序中?

If you want to know which method has more performance you can use the stopwatch class to check the time needed for each method. see here for stopwatch usage: http://www.dotnetperls.com/stopwatch

I think there are other issues for a asp.net application you need to verify:

  1. From where do read your list? if you read it from the data base, would it be more efficient to do your work in database within a stored procedure.
  2. Where is it stored? Is it only read and destroyed or is it stored in session or application?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文