带有指针的辅助变量是否有任何性能/内存影响? [英] Does an auxiliary variable with a pointer have any performance/memory impact?

查看:33
本文介绍了带有指针的辅助变量是否有任何性能/内存影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是吗?

我通常会选择这个,

NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];
[self setMonths: monthsForChosenYear];

在此之上,

[self setMonths: [self monthsForYear:newChosenYear]];

主要是因为乍一看很容易理解.第二种方法,不是那么多.

Mostly because it's easy to understand at a first glance. Where the second approach, not so much.

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

But what are really the implications of this? monthsForChosenYear, is just a pointer, but it must be stored somehow.

我不是问影响是否小到我不需要担心.但我对此非常好奇.

I'm not asking if the impact is so small that I wouldn't need to worry about it. But I am very curious about this.

即使是重定向到一些更详细解释的文档也会很好.

Even a redirect to some document explaining in better detail would be nice.

先谢谢你!

努诺

推荐答案

一个很长的答案,希望能缓和你的好奇心,有好奇心是好的!性能 &内存影响为零或微乎其微.您想知道指针是如何存储的.当您输入:

A long answer to hopefully assuage your curiosity, and having curiosity is good! The performance & memory impact is either zero or miniscule. You wonder how the pointer is stored. When you type:

NSArray * monthsForChosenYear;

您要求在本地存储中分配一个名为 monthsForChosenYear 的框(变量).当封闭方法退出时,此框将自动回收,如果编译器发现不再需要它,则可能早于此.这个框可以保存 NSArray * 类型的值.

You are asking for a box (variable), to be referred to by the name monthsForChosenYear, to be allocated in local storage. This box will be automatically reclaimed when the enclosing method exits, and possibly earlier than that if the compiler figures out it is no longer needed. This box can hold a value of type NSArray *.

当您输入时:

NSArray * monthsForChosenYear = [self monthsForYear:newChosenYear];

您要求两件事,它只是以下内容的简写:

You are asking for two things, it is just a shorthand for:

NSArray * monthsForChosenYear;
monthsForChosenYear = [self monthsForYear:newChosenYear];

第二行调用一个方法并将返回的值存储在名为 monthsForChosenYear 的框中.最后当你输入:

and the second line calls a method and stores the returned value in your box named monthsForChosenYear. Finally when you type:

[self setMonths: monthsForChosenYear];

存储在您的框中 monthsForChosenYear 中的值被传递给一个方法.如果您不再使用 monthsForChosenYear 框,编译器可能会回收它,或者它可能会等到封闭方法结束或其他合适的点.

the value stored in the your box monthsForChosenYear is passed to a method. If you no longer use the box monthsForChosenYear the compiler may reclaim it, or it may wait till the end of the enclosing method or some other suitable point.

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

Compilers analyze the usage of boxes and optimise, sometimes they will not even allocate a box if it is determined one is not needed. The cost of allocating a box is infinitesimal.

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

*[Note: there are actually usually two kinds of local boxes. The second, often called a register, has an allocation cost which is usually even smaller than infinitesimal. Which kind of box is used is decided by the compiler.]*

当您输入时:

[self setMonths: [self monthsForYear:newChosenYear]];

您要求在一行中调用 两个 方法,并且必须将内部调用返回的值 ([self monthsForYear:newChosenYear]) 传递给外部调用.编译器在哪里存储这个结果?编译器实际上将上述内容翻译成:

You are asking for two methods to be called in one line and the value returned from the inner call ([self monthsForYear:newChosenYear]) has to be passed to the outer call. Where does the compiler store this result? The compiler in effect translates the above into:

NSArray *compilerTemporaryBox = [self monthsForYear:newChosenYear];
[self setMonths:compilerTemporaryBox];

从上面你知道这是怎么回事.compiler 有一个小优势,它知道compilerTemporaryBox 需要多长时间,所以它不需要弄清楚,但这不会影响编译代码.

and from the above you know how that goes. There is a small advantage to the compiler in that it knows how long compilerTemporaryBox is needed, so it doesn't need to figure it out, but that will not effect the compiled code.

所以总而言之,总体答案是你如何编写它并不重要.

So after all that the overall answer is it doesn't matter how you write it.

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

Furthermore the type of memory management you use: MRC, ARC or GC; will not effect the answer - the compiler will end up treating both your variants the same.

所以选择你认为最适合你的风格.哈.

So go for the style you find best for you. HTH.

这篇关于带有指针的辅助变量是否有任何性能/内存影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆