回收价值类型是否统一? [英] is there a point in recycling value types unity

查看:101
本文介绍了回收价值类型是否统一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有一篇文章指出,回收和重用变量是统一的好习惯.因此,我采用了它. 但是有一件事还不清楚:这是否适用于值类型变量(整数,向量)?

I found article stating that recycling and reusing variables is good practice in unity. So I adopted it. But one thing not clear : does this apply to value type variables (integers, vectors)?

我在使用这个要点吗?

int x;
Vector3 v;
void functionCalledVeryOften(){
   x=SomeCalculation();
   v=SomeCalc();
   //do something with x and v
}

代替此:

void functionCalledVeryOften(){
   int x=SomeCalculation();
   Vector3 v=SomeCalc();
   //do something with x and v
}

推荐答案

这完全取决于您希望对该对象执行的操作.

This is fairly dependent on what you wish to do with this object.

让我们以您的第一个示例为例,假设我们要访问变量x& v来自第二个函数functionCalledEveryOnceSoOften()该函数不需要任何重载即可传递变量,并且可以直接在类的实例中访问变量.

Lets take your first example, say we would want to access the variables x & v from a second function functionCalledEveryOnceSoOften() This function won't need any overloads to pass the variables, and can just directly access the variables in the instance of the class.

在第二个示例中,如果我们想做同样的事情.我们将不得不调用functionCalledEveryOnceSoOften(int, vector3),因为该函数无法直接访问变量.

With the second example, if we wanted to do the same thing. We would have to call functionCalledEveryOnceSoOften(int, vector3) As the function would not have direct access to the variables.

通常,一个函数通常需要与另一个函数使用相同的值,尽管不一定总是在链中调用它们.为了在您的第二个示例中解决此问题,我们必须在函数内添加if语句以将其过滤掉.

In unity it is often the case that a function will need to use the same values as another function, all though they might not always be called in chain's. To accommodate this in your 2nd example, we would have to add if statements inside our function to filter this out.

但是,在您的第一个示例中,我们可以毫无问题地使用这些变量.这是经常建议这样做的原因之一.

In your first example however, we could use these variables without a issue. This is one of the reasons it is often advised to do so.

根据性能,在第二个示例中,变量存储在堆栈中而不是堆中,因为该变量是在方法范围内定义的,一旦方法结束执行,该变量将被销毁.因此,变量的内存使用量并不是真正的问题.重复创建和销毁的开销可能会很小,但这应该无关紧要.

As per the performance, in your 2nd example the variable is stored in the stack as opposed to the heap, because it is defined within the confines of a method which will get destroyed once the method ends executing. So the variable's memory usage is not really a concern. There might be a small overhead for the repeated creation and destruction, but this should be insignificant.

在第一个示例中,您将变量存储在堆中,因为它是在类范围内定义的,它只会与类一起销毁,并在实例化时创建.这意味着内存可能会使用更长的时间,但是创建/销毁变量不会有开销.这通常也无关紧要.

In your first example you will store the variable on the heap, as it is defined within the scope of the class, it will only be destroyed along with the class, and created on it's instantiation. This means that memory might be used over longer periods of time, but there will be no overhead for creating/destroying the variable. This also is usually insignificant.

总的来说,除非您实例化成千上万个此类对象,否则快速连续地访问变量,您很可能不会注意到性能上的很大差异.

All together, unless you are instantiating thousands of these objects, accessing the variables in rapid succession you will most likely not notice a lot of difference in performance.

最大的不同很可能是代码的编写方式.不论结果好坏.

The biggest difference will most likely be the way code is written. For better, or for worse.

这篇关于回收价值类型是否统一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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