Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Static class that reads from file -> performance issues?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Static class that reads from file -> performance issues?

    Hey, Thanks for your reply!

    My Parameter class would simply look like something like this:

    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:

    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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    rbk (April 11th, 2011)

  6. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Static class that reads from file -> performance issues?

    Already working on it , thanks anyway

  7. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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.

Similar Threads

  1. Issues with Static environment
    By almostlowfatmilk in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 19th, 2011, 03:26 PM
  2. issues with Class
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 20th, 2010, 01:49 PM
  3. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  4. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  5. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM