0

I produced code

for(String buff : Arrays.asList(
        FORMULA_PATTERN.matcher(s).group().split("\n")
        )

And I started wondering where performance lies with not creating an explicit local container for method returns. (Also, I'm not sure that this works, I'm just prototyping some stuff while waiting for full specs)

Related to Java coding style, local variables vs repeated method calls but I'm curious of the actuality of it.

for the coding pattern (not the specific case, just examples)

Integer.parseInt(buff.substring(buff.length()-1));

Is there any gain/loss over

int x = buff.length() -1;
Integer.parseInt(buff.substring(x);

Or, for the more general case of the language, avoiding primitives

Integer x = buff.length() -1;
Integer.parseInt(buff.substring(x);

This may differ for collections, but my curiosity is the cost associated with Java's pass by value having a cost to instantiate an object to be returned.

Not really concerned with readability, just wondering if, when, and how one method would outperform the other.

Community
  • 1
  • 1
Captain Prinny
  • 459
  • 8
  • 23
  • I doubt that there is any difference between `Integer.parseInt(buff.substring(buff.length()-1));` and `int x = buff.length() -1; Integer.parseInt(buff.substring(x);`. `int x` in the latter is probably optimized away anyway. – Jashaszun Jul 09 '15 at 18:13
  • YMMV. Test it with your compiler and VM if it's important. It's probably not important. There might be some small constant difference, but no big win. Boxing a primitive int into an Integer and then unboxing again is definitely not an improvement. In general, prefer readability over premature optimization. – Andy Thomas Jul 09 '15 at 18:19
  • Editted to clarify. I wasn't looking at those examples as SPECIFIC cases, just the difference of storing the return value of something as a box-unbox pair. – Captain Prinny Jul 09 '15 at 18:27
  • 1
    I'm having trouble following what you're asking here. Your initial example seems to include a superfluous ArrayList instantiation, the for loop can iterate right over the array. As far as use of intermediate local vars for clarity, that sort of thing has either no practical performance implication and/or is optimized away. Feel free to use them. – pvg Jul 09 '15 at 18:36
  • Probably just bad code on my part, prototyping through a bunch of solutions, it's probably remnants of the last solution I forgot to trim. I was doing character handling before moving to regex and I was having to convert between char[] and Character[]. – Captain Prinny Jul 09 '15 at 18:39

0 Answers0