简体   繁体   English

使用成员变量堆栈?

[英]Use stack of member variable?

In general is it better (for performance) to use stack variables or temporary member variables? 总的来说,使用堆栈变量或临时成员变量是否更好(为了提高性能)?

I have a function that is being called in "real time" and there are a number temporary variables (of various data types) that are required. 我有一个被“实时”调用的函数,并且有一些需要的(各种数据类型的)临时变量。 Is it better to just use stack variables (ie in function) or to use private member variables in the class? 仅使用堆栈变量(即在函数中)还是在类中使用私有成员变量更好?

My gut feeling is that stack variables would be faster, but this is a real micro-optimization. 我的直觉是堆栈变量会更快,但这是真正的微观优化。 You would have to profile it to be sure. 您必须对其进行概要分析以确保。

If they are only needed inside the method, keep them in the method as local method variables. 如果仅在方法内部需要它们,请将它们作为局部方法变量保留在方法中。 This isn't primarily a speed concern (although a "ldloc" is presumably less effort than a "ldarg0, ldfld" combination) - but rather: scope: it reduces the size of the object, and also allows for re-entrancy without confusion. 这主要不是速度问题(尽管“ ldloc”的工作量可能比“ ldarg0,ldfld”的组合要少)-而是:作用域:它减小了对象的大小 ,并且允许重新进入而不会造成混淆。

Note also: not all local method variables go on the stack ;p 另请注意:并非所有本地方法变量都在堆栈上; p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM