Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 带有指针的辅助变量是否对性能/内存有影响?_Objective C_Performance_Variables - Fatal编程技术网

Objective c 带有指针的辅助变量是否对性能/内存有影响?

Objective c 带有指针的辅助变量是否对性能/内存有影响?,objective-c,performance,variables,Objective C,Performance,Variables,是吗 我通常会选这个 NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear]; [self setMonths: monthsForChosenYear]; 在这个问题上, [self setMonths: [self monthsForYear:newChosenYear]]; 主要是因为乍一看很容易理解。第二种方法在哪里,没有那么多 但这究竟意味着什么呢?monthsForChosenYear只是一个指针,但它

是吗

我通常会选这个

NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
在这个问题上,

[self setMonths: [self monthsForYear:newChosenYear]];
主要是因为乍一看很容易理解。第二种方法在哪里,没有那么多

但这究竟意味着什么呢?monthsForChosenYear只是一个指针,但它必须以某种方式存储

我不是问影响是否如此之小,以至于我不需要担心。但我对此很好奇

即使是重定向到更详细的解释文档也不错

提前谢谢你


Nuno

Clang几乎肯定足够聪明,可以将这两段代码编译成相同的机器代码。在速度和内存方面没有区别。

您使用的是ARC吗?没有ARC,使用体面的优化器不会有什么区别,但使用ARC可能会使用额外的保留/释放。

希望能缓解您的好奇心的一个长答案,拥有好奇心是好的!对性能和内存的影响要么为零,要么微不足道。您想知道指针是如何存储的。键入时:

NSArray * monthsForChosenYear;
NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
[self setMonths: [self monthsForYear:newChosenYear]];
您要求在本地存储中分配一个名为
monthsForChosenYear
的框(变量)。当封闭方法退出时,此框将自动回收,如果编译器发现不再需要此框,则可能早于此框。此框可容纳
NSArray*
类型的值

键入时:

NSArray * monthsForChosenYear;
NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
[self setMonths: [self monthsForYear:newChosenYear]];
你要求的是两件事,这只是一个简写:

NSArray * monthsForChosenYear;
monthsForChosenYear = [self monthsForYear:newChosenYear];
第二行调用一个方法并将返回的值存储在名为
monthsForChosenYear
的框中。最后,键入时:

NSArray * monthsForChosenYear;
NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
[self setMonths: [self monthsForYear:newChosenYear]];
存储在your框
monthsForChosenYear
中的值被传递给一个方法。如果您不再使用方框
monthsForChosenYear
编译器可能会回收它,或者它可能会等到封闭方法结束或其他合适的点

编译器分析盒子的使用情况并进行优化,有时如果确定不需要盒子,他们甚至不会分配盒子。分配一个盒子的成本是微乎其微的

*[注:实际上通常有两种本地框。第二种通常称为寄存器,其分配成本通常甚至小于无穷小。使用哪种框由编译器决定。]*

键入时:

NSArray * monthsForChosenYear;
NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];
[self setMonths: [self monthsForYear:newChosenYear]];
您要求在一行中调用两个方法,并且从内部调用返回的值(
[self monthsForYear:newChosenYear]
)必须传递给外部调用。编译器在哪里存储这个结果?编译器实际上将上述内容转换为:

NSArray *compilerTemporaryBox = [self monthsForYear:newChosenYear];
[self setMonths:compilerTemporaryBox];
从上面你知道这是怎么回事。编译器有一个小优势,它知道需要多长时间
compilerTemporaryBox
,因此不需要计算出来,但这不会影响编译后的代码

所以,毕竟,总的答案是,你怎么写并不重要

此外,您使用的内存管理类型:MRC、ARC或GC;不会影响答案-编译器最终将以相同的方式处理两个变体


因此,选择最适合自己的款式。嗯。

太棒了!我希望能够就此做出明智的决定。这就做到了。非常感谢您的解释@CDR它真的很有帮助。=)