Static class that reads from file -> performance issues?
Hey there!
I wrote a static Parameters class for my project so all objects in the project would have easy access to the parameters. This worked fine until now, because I want to experiment with different parameters.
I'd prefer not to instantiate the Parameter object and give it as a parameter to each object I create (because I have to change a lot of code).
So my idea was to give the Parameters class static functions to read from a file. However, during an iteration of the program, up to a million calls could me made to the Parameters classs. Now my question is: would this hurt the performance of my system?
I'm hoping that the static class reads the file once and if it's not deleted by the garbage collector, the second time it is called it can directly return a requested value.
Thanks in advance!
Re: Static class that reads from file -> performance issues?
We can't really know that without looking at the code. And please don't post all of it- make sure it's in the form of an SSCCE.
Also, we can't really tell you about the performance on your system. Have you noticed any problems? Have you done any measuring?
Re: Static class that reads from file -> performance issues?
Hey, Thanks for your reply!
My Parameter class would simply look like something like this:
Code :
public final class Parameters {
public static final TIMESTEPS;
static {
TIMESTEPS = readFromFile();
}
}
where readFromFile() uses a Scanner.
The other code is not very convenient to post here, but I have a simulator that creates a large number of agents of different types and several other objects that currently call the Parameters in this way:
Code :
for(int i=0; i< Parameters.TIMESTEPS; i++) { }
Calls like these are made very often, so I hope that once the static class is initialized and not garbage collected, it doesn't have to read the file again. OR that reading a file is very inexpensive.
I haven't tested it yet, because I assume someone here can tell me right away what will happen in this case. And that would save me the trouble of rewriting parts ;)
Re: Static class that reads from file -> performance issues?
I would expect the file to be read only once. But, in fact, this is one of those cases where it would probably be much faster to throw together a small program that tests the theory than it would be to wait on a definite response here. Up to you.
Re: Static class that reads from file -> performance issues?
Already working on it ;), thanks anyway
Re: Static class that reads from file -> performance issues?
If you're reading the same file over and over and the file is not likely to change during runtime, just keep it's contents in memory and then return whatever you have in memory when someone calls the method. Every time you have to make an IO call there will be a performance hit so it's always best to keep that to a minimum.