简体   繁体   English

在内存和时间方面分配给String变量是否代价高昂?

[英]Is assigning to String variable costly in terms of memory and time?

String timeStamp = currentCommentObjectObj.getTimeStamp();

holder.timeStamp.setText(timeStamp);

or 要么

holder.timeStamp.setText(currentCommentObjectObj.getTimeStamp());

Which is better from a time and space optimisation perspective? 从时间和空间优化的角度来看哪个更好?

More information :- This code is inside onBindViewHolder of recycler view. 更多信息:-这段代码在回收站视图的onBindViewHolder中。

Although I prefer the second one, I believe that there is no difference. 尽管我更喜欢第二种,但我相信没有区别。 Because somewhere in the compilation steps the compiler optimizes your code and would recognize such discrepancies if any. 因为在编译步骤中的某处,编译器会优化您的代码,并且会识别出此类差异(如果有)。

Please refer to http://www.noesispoint.com/jsp/scjp/SCJPch0.htm for more information. 有关更多信息,请参考http://www.noesispoint.com/jsp/scjp/SCJPch0.htm

Apparently first javac (the Java Compiler) compiles the code to JavaByteCode and then the Java Virtual Machine's compiler JIT optimizes and compiles the Byte code to the machines language. 显然,首先javac (Java编译器)将代码编译为JavaByteCode,然后Java虚拟机的编译器JIT将Byte代码优化并编译为机器语言。

Hope that helps. 希望能有所帮助。

Regardless of the compiler/JVM behavior, this is an operation that should be essentially instantaneous. 无论编译器/ JVM的行为如何,此操作本质上都是瞬时的。

On a machine level, the only possible difference between the two methods is 在机器级别,这两种方法之间唯一可能的区别是

(a) saving a pointer to memory, then loading the pointer, or (a)将指针保存到内存,然后加载该指针,或

(b) passing the pointer directly to the next method call. (b)将指针直接传递给下一个方法调用。

The amount of time that is different between these two is so small that it essentially will never matter, for any Object. 两者之间时间的差异是如此之小,以至于对于任何对象而言,它基本上都将无关紧要。 Even in a loop, Android UI code should not be executed enough times for this to ever possibly matter. 即使在循环中,Android UI代码也不应执行足够的时间,以免发生任何事情。

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

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