0

Looking for best advice on the following optimization of many calls:

myMethod("This is a test string with value: " + var + ".");

The program has many calls with this type of behavior. Ie: this type of method is called thousands of times over of the course of program usage.

Should a cache of some sorts be built to max this optimization of string usage?

myMethod(cache.get(Cache.TEST_STRING_000001) + " + var + ".);

Tips welcomed and proper instruction on best practices and efficieny is also welcomed.

1 Answers1

2

No. That is completely useless. Each time this code is executed, the same String instance is used for "This is a test string with value: ".

Even more, if the same String literal is used in 10 different classes, a unique instance is used in all classes.

Your code would be less readable, especially with such a meaningless name as TEST_STRING_000001.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Basically agree. We'd need to know a lot more about the OP's code, and how performance measurements are being taken, before "optimizing" anything remotely close to this. – markspace Feb 03 '15 at 20:40
  • Is a command line program for processing various activities across a network of users. Many messages are displayed and am using the hardcoded as arguments in the methods. Due to the number of hard coded Strings and methods, thought maybe improvements could be made and do not fully understand how a String argument behaves. Each string is unique to a certain class, most times. ie: myMethod appears in ClassA and otherMethod appears in ClassB. – dustOfNothing Feb 03 '15 at 21:18