开发者

Reducing unnecessary same values in Class member variables

class A
{
 public int a;
 public int c;
}
开发者_StackOverflow中文版

I will create 10 instances from A.Then I will create 15 instances from A again... go on. first 10 instance will have same value for a variable and next 15 instances will have again same value for a.But I don't mean that both group has same values for a .Problem is create same a value 10 times in first group and 15 times in second group on memory unnecessary.

What would be Best solution or solutions for reduce unnecessary datas in this situation?


If a and c are actually just integers, it won't be worth your time trying to optimize them out of the memory space; in most cases, anything you would use to do so would take up more space than the integers themselves.

However, if a and c are actually objects that take up a significant amount of memory, you could instead put object pointers (or holders, depending on the language) as members of A instead of the objects themselves. That way the only memory duplicated in the pointer.


It is clear that static member will not work for this problem.Because instance groups will have different values.

this can be one of the solutions.What can other else ?

class Common
{
  public int a;
}
class A
{
  public Common a;
  public int c;
}


Another solution is

class A
{
  public int? a;
  public int c;
}

but instead of create new instance of int? class we should to assign same instance.But casting int? to int will be ugly to use and cause performance issues.


Would a HashSet<T> help?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜