内存传递方法参数的最佳方法 [英] Best way for memory to pass method parameters
问题描述
我的代码遇到一些内存性能问题,我不确定哪种方法是将参数传递给我的方法的最佳方法.我将举一个简短的例子.
I am experiencing some memory performance issues with my code and I am not sure which is the best way to pass parameters to my methods. I will give a short example about this.
例如,我有此类:
class Person
{
public string Name { get; set; }
public bool IsMale { get; set; }
public string Address { get; set; }
public DateTime DateOfBirth { get; set; }
}
然后我就这样使用它:
static void Main(string[] args)
{
Person person = new Person {
Name = "John",
Address = "Test address",
DateOfBirth = DateTime.Now,
IsMale = false };
int age = CalculateAge(person);
int age2 = CalculateAge(person.DateOfBirth);
}
private static int CalculateAge(Person person)
{
return (DateTime.Now - person.DateOfBirth).Days;
}
private static int CalculateAge(DateTime birthDate)
{
return (DateTime.Now - birthDate).Days;
}
现在在我的应用程序中,我有很多方法调用,其中将整个对象(具有很多属性的对象不像示例中的"Person"对象那样)作为参数传递,我正在考虑通过发送给这些方法只是它们所需的属性,但是目前我不确定这将如何提高内存性能.
Now in my app I have lots of method calls where the entire object (an object with lots of properties not like this "Person" object from my example) was passed as parameter and I am thinking to improve the code by sending to the methods only the properties that they need, but currently I am not sure how this will improve memory performance.
关于内存使用情况,将整个Peron对象发送到我的方法(如CalculateAge(person);
)还是仅发送用于该方法的属性(如CalculateAge(person.DateOfBirth);
)如何更好?
How is it better regarding memory usage, to send the entire Peron object to my method like CalculateAge(person);
or to send just the property that is used into the method like CalculateAge(person.DateOfBirth);
?
首先,我认为像这样的CalculateAge(person.DateOfBirth);
调用(仅将所需的属性而不是整个对象作为参数发送)占用的内存较少,但最终在测试后我注意到该应用的执行速度较慢,现在我不确定如果这些更改或其他更改减慢了我的应用的速度.
First I thought that calls like this CalculateAge(person.DateOfBirth);
(sending as parameters only the needed properties instead of the entire object) are using less memory but in the end after tests I noticed that app performs slower, and now I am not sure if these changes or others were slowing down my app.
推荐答案
在这里要考虑三种情况:
There are three situations to consider here:
- 传递
class
对象与另一个class
对象 - 传递
struct
值与class
对象 - 传递多个
class
/struct
与单个class
- Passing a
class
object vs. anotherclass
object - Passing a
struct
value vs. aclass
object - Passing multiple
class
/struct
s vs. a singleclass
就内存而言,只有第一种情况将成为平等交易:传递string Name
所花费的内存与传递Person person
所花费的内存一样.
In terms of memory only the first case is going to be an equal trade: it takes as much memory to pass a string Name
as it takes to pass Person person
.
通过struct
,例如DateTime
,可能会占用更多的内存或更少的内存,具体取决于struct
的大小.对于DateTime
,大小为(当前)八个字节.对于较小的按值对象,例如一个带有单个short
的struct
,您将需要更少的字节.对于较大的struct
,例如DateTimeOffset
,您将需要更多字节.
Passing a struct
, such as DateTime
, may take more memory or less memory, depending on the size of the struct
. For DateTime
the size is (currently) eight bytes. For a smaller by-value object, e.g. a struct
with a single short
, you would need fewer bytes. For larger struct
s, e.g. DateTimeOffset
, you would need more bytes.
在大多数情况下,传递多个对象或值将比传递单个class
对象需要更多的内存.唯一的例外是传递几个非常小的struct
.
Passing multiple objects or values in most cases is going to require more memory than passing a single class
object. The only exception would be passing several very small struct
s.
独立于考虑内存影响,您应该在决定策略之前考虑API的逻辑结构.
Independently from considering the memory impact you should consider the logical structure of your API before deciding on the strategy.
将Person
传递给计算年龄的API会在年龄计算API和Person
之间建立依赖关系.另一方面,分别传递DOB会在Person
和API的调用者之间创建依赖关系.
Passing a Person
to an API that computes the age creates a dependency between the age-computing API and the Person
. Passing DOB separately, on the other hand, creates a dependency between Person
and the caller of the API.
如果您打算仅将年龄计算API与Person
对象一起使用,则传递Person
是有意义的.此外,您应该考虑将计算属性直接添加到Person
类,
If you plan to use age-computing API exclusively with Person
objects, then passing Person
makes sense. Moreover, you should consider adding a computed property directly to the Person
class,
另一方面,如果您打算将年龄计算API与其他对象(动物,建筑物等)一起使用,则最好不要传递DOB.
On the other hand, if you plan to use age-computing API with other objects (animals, buildings, etc.) you would be better off passing DOB.
这篇关于内存传递方法参数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!