0

I'm just wondering why the performance is so bad when working with generic Lists and Arrays in Java.

I originally had the below to create a generic array, but it was taking over 2 seconds to create the array with 50 elements.

final T[] returnClasses = (T[]) Array.newInstance(classOfT, data.getCount());

After reading a number of areas that say java.lang.reflect.Array shouldn't be used as it hasn't been optimised and to use Lists instead I changed my code to the below.

List<T> returnDataList = new ArrayList(data.getCount());

After this change there was no performance increase.

When comparing the timings with creating an explicitly typed list the times returned were always 0ms

What is the most efficient way to create a collection (must retain order) when using generics?

If it makes a difference the method is declared as

public <T> List<T> method(Data data, Class<T> classOfT)

Logging was done with the below code

Calendar now = Calendar.getInstance();
List<T> returnDataList = new ArrayList(data.getCount());
Log.d("TEST", "create array time = " + (Calendar.getInstance().getTimeInMillis() - now.getTimeInMillis()));

now = Calendar.getInstance();
List<Object> test = new ArrayList(data.getCount());
Log.d("TEST", "create test array time = " + (Calendar.getInstance().getTimeInMillis() - now.getTimeInMillis()));

I understand this form of logging isn't the correct method for proper performance testing, but it's a super simple way to get an idea of the differences between two very basic pieces of code.

Stimsoni
  • 3,166
  • 2
  • 29
  • 22
  • 3
    How are you measuring the time ? – 11thdimension Apr 19 '16 at 02:58
  • calling the ArrayList constructor takes basically no time. Are you sure `data.getCount` is not the culprit? – Thilo Apr 19 '16 at 03:00
  • 1
    it's impossible. you should post all your code. – Jason Hu Apr 19 '16 at 03:04
  • Very basically, Just logging with a calendar object. I understand this is not strictly accurate as logging itself is slow, but it does give an idea of relative speed as the standard List can be initiated in 0ms and List takes over 2000ms. – Stimsoni Apr 19 '16 at 03:05
  • Thilo: data.getCount() was used for both List and List so the only difference was the generic type. – Stimsoni Apr 19 '16 at 03:06
  • The times you are getting are undoubtedly due to something you haven't accounted for. With processor speeds today, 2 seconds is a ridiculously long time. – Radiodef Apr 19 '16 at 03:06
  • 1
    @Stimsoni the only difference is not the generic type, since all generic information is lost at runtime. the only difference is how you time the code before and after. – Jason Hu Apr 19 '16 at 03:08
  • Updated with the logging code. Is this effective at all?? – Stimsoni Apr 19 '16 at 03:10
  • 1
    Also see http://stackoverflow.com/q/504103/2891664. If you write a correct microbenchmark, the disparity will probably disappear completely. – Radiodef Apr 19 '16 at 03:12
  • Thanks RadioDef I'll take a look at getting proper times. I think there may still be an issue though as I have run my code with just the list initialisation being commented out and the performance issue disappears. – Stimsoni Apr 19 '16 at 03:15

0 Answers0