Sign in to follow this  

Reading external code

This topic is 1192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by Madolite

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by TheChubu

Share this post


Link to post
Share on other sites

This topic is 1192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this