简体   繁体   English

C#-与字符串属性重用对象与创建新实例的性能

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

Suppose I have class A that contains 3 string properties. 假设我有一个包含3个字符串属性的类A。 New instances of A are being created several times needlessly throughout the application. 在整个应用程序中,不必要地多次创建A的新实例。 All class A does is use these 3 strings to perform a LINQ query like: A类所做的全部工作就是使用这3个字符串来执行LINQ查询,例如:

Where(string1 == this.string1).Where(string2 == this.string2).Where(string3 == this.string3) 其中(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? 有没有一种更快的方法来执行上述LINQ查询呢?

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. 仅当您创建的应用程序如此之多,以至应用程序不得不频繁执行垃圾回收来清理未使用的对象时,对于具有3个属性的对象,除非字符串非常大,否则它的数量级必须达到数百万。

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. 外部对象仅包含3个指针。

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. 这意味着优化那些已被测量为问题区域的事物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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