Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance impact for using code blocks in Java?

Tags:

java

opengl

lwjgl

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?

like image 596
Charlie Mulic Avatar asked Dec 17 '22 01:12

Charlie Mulic


1 Answers

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!

like image 103
templatetypedef Avatar answered Mar 06 '23 20:03

templatetypedef