Reputation: 1
this question is about java coding, my question is easy so i'll explain it with a little example :)
I have a String array full of arguments, and i have to check it's length several times, so, it's better to save the amount of arguments (array length) in a variable, or just call the length every single time?, i suppose that storing a variable will consume more memory, and the other way will use more cpu, just want to ask what do you think is better thanks!
Example:
String[] arguments = {"a","b","c"};
int numberOfArguments = arguments.length;
if(numberOfArguments == 1)do whatever
if(numberOfArguments == 2)do whatever
if(numberOfArguments == 3)do whatever
etc
OR
String[] arguments = {"a","b","c"};
if(arguments.length == 1)do whatever
if(arguments.length == 2)do whatever
if(arguments.length == 3)do whatever
EDIT: i know the difference will be really small.. but i'm asking for a LONG PROCESS, think big, a million of "arguments.length", so for that case i think is better to sacrifice a ultra small amount of memory to store the variable which should be better for the cpu than checking the arguments lenght a million of times than checking a variable value which is always the same, what do you think?
Upvotes: 0
Views: 118
Reputation: 129487
This is a premature optimization; do whatever you find more readable/easier to manage. Personally, I prefer to just refer to the length
field directly because I see no point in storing it in another variable (unless, perhaps, the length of the array has a different meaning in the context of your program than simply a count of elements - in such a case it might be appropriate to store it in a reasonably named variable). In any case, you can rest assured that there will be no appreciable performance difference between the two.
By the way, array.length
might even be faster in some cases (assuming you're able to see any time difference whatsoever) than storing the length in a local variable and using that instead. There's even a bytecode operand reserved specifically for this: be
(arraylength
), not to mention the optimizations that will be made along the way.
Upvotes: 4
Reputation: 3709
The difference in both is creating one extra variable versus getting length of array, the difference will depend on how you are using this code.
If you create a millions instances of this class, then in first approach you will be creating million variable and you can now think of memory being used more in first approach.
Reading length of array is not expensive, so in nutshell it really depend on how is this code being used in your project.
Leaving everything aside, since length is being used multiple times creating a variable is a good practice.
Cheers !!
Upvotes: 0
Reputation: 5259
Object oriented programming saves you having to do long if statements. Calculate the number of arguments once, and create an instance of the specific class for that scenario. Then you can call any method on that instance and it will act in a 'do whatever' manner for that situation.
(forgive my rusty Java...)
public interface Arguments{
void foo();
}
public class OneArgument implements Arguments{
public void foo(){
// do whatever
}
}
public class TwoArguments implements Arguments{
public void foo(){
// do whatever
}
}
public class ThreeArguments implements Arguments{
public void foo(){
// do whatever
}
}
Then you could have a factory method:
public static Arguments create(String[] args){
if(args.length == 1) return new OneArgument();
else if(args.length == 2) return new TwoArguments();
else if(args.length == 3) return new ThreeArguments();
else throw new Exception();
}
Better thought out architecture will most likely yield better performance in the long run.
Upvotes: -1
Reputation: 44439
First of all: you can't compare storage with speed, those are different things. What you can do is determine how much you value each. If you need 100% speed, you will go for the speed option and vice versa.
Usually you want an equal consideration, which brings us to this: it's up to your own preference. Will you often access that property? Maybe it's interesting to store it in a variable.
See for yourself what you find easier to use: your example has very little influence either way. Retrieving the size of a list is already stored in a variable so there's no looping going on.
Upvotes: 1