0

I believe there are many similar questions, sorry if this is too common. I want to learn which one is better/faster/space efficient etc and why.

public static void(String[] main){
    //case 1
    String[] str_arr = new String[n];
    method1(str_arr)

    //case 2
    String[] str_arr = new String[n];
    String[] arr = new String[n];
    for(int i=0; i < n; i++){
        arr[i] = str_arr[i].split("some_char")[2];
    }
    method2(arr);
}

void method1(String[] str_arr){
    String[] arr = new String[n];
    for(int i=0; i < n; i++){
        arr[i] = str_arr[i].split("aChar")[2];//assume there are 50 of aChar 
    }
//  do_something with arr ;
}

void method2(String[] arr){
//  do_something with arr ;
}

Which one should I prefer?

Thanks in advance.

smttsp
  • 4,011
  • 3
  • 33
  • 62
  • Which one is more convenient? – user253751 Feb 18 '15 at 07:43
  • Having a method that only calls a method is kind of pointless. – Jørgen R Feb 18 '15 at 07:45
  • @immibis, if both are equal, I will choose `method1` because main method looks tidier. Also, I may not need to create `String[] arr`, I will create a `String` and do the process on it. – smttsp Feb 18 '15 at 07:50
  • @jurgemaister, I editted the code, I didn't pay attention to that line. – smttsp Feb 18 '15 at 07:51
  • @smttsp Well then, you've answered your own question - use `method1`. – user253751 Feb 18 '15 at 07:54
  • @immibis, I didn't get answer for my question yet, are they equal or which one is faster? Consider that I'll call one of these methods millions of times, more importantly, I want to learn which one is faster – smttsp Feb 18 '15 at 07:56
  • @smttsp Neither one is faster. – user253751 Feb 18 '15 at 07:57
  • Now, my question should be more precise. – smttsp Feb 18 '15 at 07:58
  • 1
    Store your `split()` result in a new variable, because it work with `regexp`, and `regexp` is so slow. – TEXHIK Feb 18 '15 at 08:00
  • @TEXHIK, Why should it be so slow? When I run `split()`, it saves the output to a temporary `String[]` and I'm reading the third element in the array. I call the method only once in both how I do and how you say I should do, isn't it? – smttsp Feb 18 '15 at 08:33
  • @smttsp, I read your code incorrect, sorry. I think you split same string every time, but now i see, that it's different strings. So you are right. – TEXHIK Feb 18 '15 at 08:38

1 Answers1

1

This is purely up to your discretion.

When it comes to performance reasons:

  • Use enhanced for loops, where you can, as they can sometimes impact the loop performance (see a nice micro-benchmark here: Java Enhanced-For-Loop FASTER than traditional?), or...
  • Do not create additional storage array if you do not need the originals anymore. Simply read the value, split() it and store it back. On very large arrays, you'll save yourself some allocation time and space.
  • In this particular example, the actual cost of method invocation is neglible. If you're extra paranoid (not recommended) you could make the method static to prevent being it thiscall function.

In terms of clean and tidy code:

  • Your method looks like it may be something generic so it's a great candidate for just a method that takes an array, along with the delimiter, and splits all its elements.
  • main() code is by convenience advised to be as small and compact as possible.

TL;DR Go for the first option, and eventually make some modification.

Also, it may be a good idea to split the elements before actually putting them inside the array.

Community
  • 1
  • 1
Jitsu
  • 769
  • 3
  • 7