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.