简体   繁体   English

Java相当于寄存器int?

[英]Java equivalent of register int?

In C, I can allocate a register for a variable, for example: 在C语言中,我可以为变量分配一个寄存器,例如:

register int i = 0;

I am aware that Java is an interpreted language, and is many many abstractions away from the CPU. 我知道Java是一种解释型语言,并且有许多抽象的CPU形式。

Is there any mechanism available to even request (and if the architecture doesn't allow it, so what) that my variable remains in a register instead of moving to cache or main memory? 是否有任何机制甚至可以请求我的变量保留在寄存器中而不是移至高速缓存或主存储器(如果架构不允许,那么该怎么办?)?

I don't suppose there is any way, but I have been pleasantly surprised before. 我不认为有任何办法,但是以前我感到惊喜。

Thank you, 谢谢,

register in C does not put a variable to register. 在C中register不会将变量注册。 It simply gives the compiler the hint, that it would probably be good to put it into a register. 它只是给编译器一个提示,将其放入寄存器可能会很好。

In Java there is no equivalent. 在Java中没有等效项。

If it's used enough in a short space that making it a register int would be worthwhile, then the hotspot compiler should be able to figure that out itself. 如果在较短的空间中使用了足够的内存,使其成为一个寄存器int是值得的,那么热点编译器应该能够弄清楚这一点。

In fact, the hotspot compiler should be able to do a better job than the C/C++ compiler, because it has more information to work with. 实际上,热点编译器应该能够比C / C ++编译器做得更好,因为它具有更多可使用的信息。 C/C++ compilers have to guess; C / C ++编译器必须猜测; HotSpot can measure. 热点可以衡量。

There's no equivalent in Java. Java中没有等效功能。 Even in C there is no guarantee that the variable will be stored in a register and compilers are free to ignore it. 即使在C语言中,也无法保证将变量存储在寄存器中,并且编译器可以随意忽略它。

In Java, the method will be interpreted until the hotspot JIT heuristically determines that it needs to be compiled. 在Java中,将解释该方法,直到热点JIT启发式确定需要对其进行编译为止。 For compiled code it uses a coloring algorithm to assign variables and temporary values to registers, or write to/from RAM in the case of register overflow. 对于已编译的代码,它使用着色算法将变量和临时值分配给寄存器,或者在寄存器溢出的情况下从RAM写入数据或从RAM写入数据。

No, there's no way to request this in Java. 不,没有办法用Java请求。 However, there are some things that you can do that will prevent a register from being used, such as applying the volatile modifier to a member variable. 但是,您可以做一些事情来防止使用寄存器,例如将volatile修饰符应用于成员变量。

I am aware that Java is an interpreted language, and is many many abstractions away from the CPU. 我知道Java是一种解释型语言,并且有许多抽象的CPU形式。

You pretty much answered your own question there :-) 您几乎在那里回答了自己的问题:-)

But seriously, in general, write your code as clearly and simply as you can, and the JVM will do what it can to treat your code right. 但是,认真的说,总的来说,请尽可能清晰,简单地编写您的代码,并且JVM会尽其所能正确对待您的代码。

You can create an annotation called @register but the JVM will definitely ignore it. 您可以创建一个名为@register的注释,但是JVM肯定会忽略它。 eg 例如

@register int i = 0;

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

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