Advertisement

Reading external code

Started by September 07, 2014 01:27 AM
2 comments, last by Madolite 10 years, 4 months ago

I'm assuming that reading code from another class is marginally slower than reading code from within the same class, since Java needs to actually get that .Java-file to run the code? As a self-taught, this is one of those obscure (and perhaps completely unimportant) things that I've yet to see mentioned anywhere specifically (to get confirmed). Thinking logically though, it sounds like it's not even the least important performance-wise.

So to be sure, are there any cases at all (performance-wise) where reading from the same class is preferred over reading from another class, or am I "go-for-launch" however I please and just structure my code according to cleanliness and ease of reading?

Thanks in advance.

Don't worry about it, create files as you need and do things the way it makes things more confortable to you.

In the end the code is not read from each file every time you call some function, there are lots of things happening, but accessing the classes reading from separate files isn't one of them. When you compile Java code some bytecode is created, and the PC will load pieces (or all, if possible) of it into ram, so accessing different parts of your code is actually a memory access. Also, Java and other languages create an internal structure to hold the different classes definition and implementations in memory, so the files you created don't even exists to the running app.

Advertisement

The worst "performance hit" you could have is that the JVM has to load the .class file into memory the first time you reference it (wheter its a static field, or you initialize an object of that class, etc) and run any static initializer blocks you have in it.

But that operation is done only once per class and only when you do something with it, JVM won't load classes that you don't touch.

And as Diego said, its all memory accesses, as costly as any other memory access. ie, if you have two objects of different classes, and you access an int field of one object and an int field of the other object, access cost will be probably the same since they're memory accesses (or if you're lucky, the thing is in CPU cache and it will be much faster, it isn't something you can control precisely in Java).

So don't try to cram stuff into a single class because you think it will run faster.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Alrighty, thanks again guys. :)

This topic is closed to new replies.

Advertisement