简体   繁体   中英

C# - Performance of reusing object with string properties vs creating new instance

Suppose I have class A that contains 3 string properties. New instances of A are being created several times needlessly throughout the application. All class A does is use these 3 strings to perform a LINQ query like:

Where(string1 == this.string1).Where(string2 == this.string2).Where(string3 == this.string3)

Instead of creating an instance every single time the object is needed, I'm thinking about modifying the classes that use this type to store one instance of the object and then modify the string properties before using it each time. Is this the right way to optimize the class? Basically, I'm trying to avoid the overhead of creating the instance and allocating the memory for the strings every time.

Is there a faster way to perform the above LINQ query instead?

Yes it is. But unless there're plenty of those objects, the difference in performance will be negligible.

If "several times" is on the order of a few thousand then I wouldn't worry about the performance of creating the objects. Only if you are creating so many that the application is having to frequently perform a garbage collection to cleanup the unused objects, which for an object with 3 properties would need to be on the order of millions unless your strings are extremely large.

You are more likely to create bugs by trying to reuse an object, since you run the risk of forgetting to clean object state between reuses.

Trying to reuse the object will likely mean moving it to a higher level scope than necessary. Ie changing a local reference to a property, or similar.

Keep your code simple, rather than implement premature optimizations, such that when you do get into a situation where optimization is really important, then you can more easily implement optimizations. If you over optimize everything you can imagine, then you will end up with an over complicated code base.

Update:

I'm thinking about modifying the classes that use this type to store one instance of the object and then modify the string properties before using it each time. Is this the right way to optimize the class?

You're not saving much in the realm of memory allocation. Since you are assigning new strings each time, most of the allocation is in the creation of the strings, not in the creation of the object.

The outer object consists of little more than 3 pointers.

Whether you reuse the same object repeatedly, or not, the majority of memory allocation is the creation of strings each time.

If you are going to put effort into optimization, your efforts should be focused where they will be most beneficial. Efforts devoted to optimization should begin with profiling to determine where your slow points are. This means optimizing those things that have been measured to be problem areas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM