开发者

code performance question

开发者 https://www.devze.com 2023-03-10 13:05 出处:网络
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-20M

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 retur开发者_JAVA百科ns 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.


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!


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?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号