-2

possible duplicate

As question was asked How to convert Vector to String array in java

which one is the best performance for store the vector value to String array. And how it will perform in background?

Vector<String> v = new Vector<String>();
// this one
String s[] = v.toArray(new String[v.size()]);
// or this one
String s[] = v.toArray(new String[0]);
3
  • Time a million of each and find out. Commented Sep 21, 2011 at 13:35
  • Why don't you benchmark them and see for yourself? Commented Sep 21, 2011 at 13:35
  • 2
    Using a Vector when you should be using an ArrayList is likely to make more difference. Commented Sep 21, 2011 at 13:48

2 Answers 2

2

As a general rule of thumb, it's faster to properly size an array or collection the first time, because it prevents the need to resize it one or more times later. Less work == faster. BTW, you almost certainly shouldn't be using a Vector; an ArrayList is a better choice in the vast majority of cases.

UPDATE: For your particular case, the results are going to be heavily dependent on the data in your Vector. As with any question about performance, you should profile it for yourself. Since you've said you don't know how to benchmark, I suggest you read up on profiling and performance measurement for Java applications. If you aren't measuring things, you shouldn't waste your time worrying about the performance of standard library operations. You're likely worrying about things that have nothing to do with the actual bottlenecks in your applications.

Sign up to request clarification or add additional context in comments.

Comments

2

I had expected new String[list.size()] to be fastest, however this appears to result in an extra lock for Vector, making it slower.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class Main {
  private static final String[] NO_STRINGS = {};
  private static final int runs = 50000000;

  public static void main(String... args) {
    List<String> strings = Arrays.asList("one,two,three,four,five,six".split(","));
    List<String> arrayList = new ArrayList<String>(strings);
    Vector<String> vector = new Vector<String>(strings);

    testNoStrings(arrayList);
    testStrings0(arrayList);
    testSize(arrayList);

    testNoStrings(vector);
    testStrings0(vector);
    testSize(vector);
  }

  private static String[] testSize(List<String> list) {
    String[] ret = null;
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
      ret = list.toArray(new String[list.size()]);
    long time = System.nanoTime() - start;
    System.out.printf(list.getClass().getSimpleName() + " Using new String[list.size()] took an average of %,d ns%n", time / runs);
    return ret;
  }

  private static String[] testNoStrings(List<String> list) {
    String[] ret = null;
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
      ret = list.toArray(NO_STRINGS);
    long time = System.nanoTime() - start;
    System.out.printf(list.getClass().getSimpleName() + " Using NO_STRINGS took an average of %,d ns%n", time / runs);
    return ret;
  }

  private static String[] testStrings0(List<String> list) {
    String[] ret = null;
    long start = System.nanoTime();
    for (int i = 0; i < runs; i++)
      ret = list.toArray(new String[0]);
    long time = System.nanoTime() - start;
    System.out.printf(list.getClass().getSimpleName() + " Using new String[0] took an average of %,d ns%n", time / runs);
    return ret;
  }
}

Any difference you see is highly likely to be machine dependant, however one obvious factor is that ArrayList is faster than Vector.

ArrayList Using NO_STRINGS took an average of 17 ns
ArrayList Using new String[0] took an average of 22 ns
ArrayList Using new String[list.size()] took an average of 27 ns
Vector Using NO_STRINGS took an average of 28 ns
Vector Using new String[0] took an average of 29 ns
Vector Using new String[list.size()] took an average of 46 ns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.