I'm starting to play with OpenGL from Java, and I'm encountering a situation where I need to put a lot of code between many glBegin() and glEnd() calls, and would like for the code to be be auto formatted such that it is easy to see at a glance which code belongs to which glBegin/glEnd.
To accomplish this, I have been using an anonymous code block, like this:
glBegin(GL_QUADS);
{
glVertex2f(100, 100);
glVertex2f(100+200, 100);
glVertex2f(100+200, 100+200);
glVertex2f(100, 100+200);
}
glEnd();
My question is: are there any performance concerns, even if extremely minor, for using code blocks in this manner? Or is it identical to not using the code blocks at all once the program is compiled?
There should be no cost for using blocks like this. Blocks are a syntactic feature of the language used for scoping purposes and have no associated runtime functionality. Looking at compiled bytecode executed by the JVM, there's no way to tell what the scoping rules of a function are, and so the JVM should give the same performance with and without the blocks.
Feel free to do this if you think it's easier to read. In fact, that should almost always be your priority, unless you have a reason to suspect otherwise.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With