Instantiating an object to make code faster [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a Utils class that loads a large list of Strings (a static variable) and defines a function that uses this list.
I use this function in another class Solution. Currently, I am calling Utils.my_function every time I use it (in a big for loop, so it is called thousands times). Would it be faster if I instantiate a Utils in Solution? (would the list of words defined in Utils be defined only one time?)

a large list of Strings (a static variable)
...
would the list of words defined in Utils be defined only one time?
By definition, a static variable is loaded only once. So it's already the fastest you can do.
EDIT : the devs who code Java are smart. It's very likely that the JVM can detect your array is accessed very often, and will optimise its operations, whether it is a static or instance variable. However I cannot give you more information than this, and maybe some Java experts can give you a more accurate answer.

Well, as already pointed out that static is only called once anyway. Another thing you can do is batch processing -- http://java.dzone.com/articles/batch-processing-best
Instead of looping by each line -- call a set of lines at a time then perform your functions, then move to the next set, etc. You would need to profile your app to see how many lines would yield a good response for the app.

Related

what is the purpose of using System.out.println() [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I was going through the internal implementation of System.out.println().Though I understood how this is working but couldn't figure out :
Why they decided to use the System class in the first place.
They could have directly used PrintStream class which is present in the io package.
What is the significance of the syntax className.referenceVariable.Methodname, since we generally don't use this. Is there any specific reason for this.
Can anybody elaborate on these points or any related information would be great.
According to my understanding, System class name was to indicate any interactions with System on which JVM is running. To perform Read or Write operations on the System Console, or reading environment variables in the System etc.
You can always use PrintStream directly as well there is no harm. But then you have to create a PrintStream object every time in your class and call methods inside use it. Instead its already created statically in System class for us to use it readily.
As for your 3rd query, Eran already answered it in the comments.
The System class states that:
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Since you were checking the source code of System class, you might've noticed that out object is final and is set to null. It's the setOut() method which assigns a value to the out variable (it's a native code).
I know, how can JVM set value to a final variable after it's been set to null, right? Being a JVM has its own advantages!
Before a value gets assigned to out object, a separate method called checkIO is also triggered, which checks for IO permission.
So System class was designed as a collection of standard input, output, and error streams. It also instructs JVM to initialise the objects, like the out object.
Regarding the syntax of System.out.println(), Eran has already explained it.

A more effective way to check if several values are true [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a body of code that has to insure a file exists and if it does load it to the system to be read if it is not true it will create the file in the proper format and save it. I have to do this numerous times and thus i created a function. I was wondering if there is a way to take these numerous similar if statements and cut them down so that it doesn't utilize as large of an if statement set up
Sample code:
//Currently my code calls a function called CheckVal to check a file and a file where the data type exists.
//the call looks like this:
public Object CheckVal (File valPath, File fullPath)
{
//this will then check if it exist using .exists()
if (valPath.exists)
{
//load Value
}
else
{
//Create value
}
return Value;
}
This data is called several times hence why I wish to cut down on the number of calls to these functions to reduce the overall time the program may take. I was hoping a solution may be possible as even the smallest time difference can greatly impact the speed of the program.
Edit:
For some clarification all of this method above is currently called roughly 6 times per run of a for loop which can be run multiple times using all the files in a certain directory. As such the impact small differences in length of computation can create large differences in the greater outcome of the program. In regards to the file directory it is not itself called 6 times; instead it is used as an extension and is altered to place the file in different folders. Thus this code is not inherently repetitive as it does not call the same directory but different variants to ensure that they are all of a type (Object) for computation later.
Using a ternary operator could be nice but i don't know if it would really improve performances anyway you can try this:
return valPath.exists() ? loadvalue() : createvalue();
You need to create two methods that create or load the value and return it.

Java call performance vs search performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.

Why are static variables bad? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've heard so many different opinions, and haven't really been able to find a solid answer to this question. When/why are static variables bad?
If I wanted to store names of players in a game in a List, would it be better to make it non-static than static? If so, why?
The list would be updated everytime a player leaves/joins. It would be used to show the players who else is playing and to track their play time.
Static variables aren't necessarily bad, but a major principle of software design is that you should contain information to the smallest context that needs to know it. If it really makes sense for a variable to be shared among all instances of a class--logger objects are a common example--then making it static is just fine. If it would ever make sense to have more than one version or copy of it, as it would in the case of the players of a game (think multiple games running simultaneously), then it's best to place the list in the smallest possible (non-static) context.
Static variables make it more difficult to see the data as the input/output of functions, because any function has access to a static. It becomes an issue when you get thousands or tens-of-thousands lines of code. Not so much in your first small app.
It's also easier to "leak memory" because the life of a static is the same as the program, meaning it never gets garbage collected till the pogram closes, or it's nulled by code.

Linked Lists - Efficient usage? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I've been giving a project for school and was just hoping for reassurance towards the decision making I do.
My java skills are extremely basic and need to improve drastically soon! I just seem to be getting nowhere atm :P
Anyway, back on topic.
My first task is to create an interface directory that can do the following
Keep in mind this is assessed work therefore please don't provide answers. I have enough time to complete this since I've started early!
My initial approach for going about this task is using a Linked List. I don't know what you guys think about that? I may be completely wrong but based on the topics we have covered in School. LinkedList definitely seems suitable. I can add, get and remove.
Cheers for reading guys!
You are along the right lines. The java.util.Collections package will contain most of what you need.
I would actually use an ArrayList rather than a LinkedList as it is faster for random access and sorting.
However note that it says you should be able to find people efficiently and look them up by name.
That suggests using something like a TreeMap structure, mapping name to a class containing information on each person. Store the names as "Surname, Forename" and they will be sorted correctly.
That will only allow lookups based on the complete and correct name though. If you want to search for partial names the map is less useful.

Categories

Resources