Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String args[]){
- ThreadMXBean bean = ManagementFactory.getThreadMXBean();
- DecimalFormat formatter = new DecimalFormat("#,###");
- DecimalFormat formatter2 = new DecimalFormat("#,###.##");
- long[] log = new long[20];
- long startTime, finishTime, mem1, mem2;
- for(int r = 0; r < 10; r++){
- startTime = bean.getCurrentThreadCpuTime();
- for(int x = 0; x < 1; x++)
- function1();
- finishTime = bean.getCurrentThreadCpuTime();
- mem1 = (finishTime - startTime);
- startTime = bean.getCurrentThreadCpuTime();
- for(int x = 0; x < 1; x++)
- function2();
- finishTime = bean.getCurrentThreadCpuTime();
- mem2 = (finishTime - startTime);
- log[r*2] = mem1;
- log[r*2+1] = mem2;
- System.out.print("f1: " + formatter.format(mem1) + " ns\t");
- System.out.print("f2: " + formatter.format(mem2) + " ns\t");
- System.out.print("difference: " + formatter.format(mem2 - mem1) + " ns\t");
- System.out.println((int)((double)mem2 * 100 / (double)mem1 - 100) + "%");
- }
- System.out.println("Average:");
- mem1 = 0;
- for(int x = 0; x < 20; x+=2)
- mem1 += log[x];
- System.out.print("f1: " + formatter.format(mem1 / 10) + " ns\t");
- mem2 = 0;
- for(int x = 1; x < 20; x+=2)
- mem2 += log[x];
- System.out.print("f2: " + formatter.format(mem2 / 10) + " ns\t");
- System.out.print("difference: " + formatter.format((mem2 - mem1) / 10) + " ns\t");
- System.out.println(formatter2.format((double)mem2 * 100 / (double)mem1 - 100) + "%");
- }
- private static String str = Arrays.deepToString(new double[10][20][30][40][50]);
- private static void function1(){
- reverseDeepToString(str, double.class);
- }
- private static void function2(){
- reverseDeepToString2(str, double.class);
- }
- private static String[] arraySeparators;
- private static Class[] arrayTypes;
- public static <T> Object reverseDeepToString(String str, Class<T> dataType){
- int dimensions = 0;
- while(str.charAt(dimensions) == '[')
- dimensions++;
- arraySeparators = new String[dimensions + 1];
- String separator = ", ";
- for(int x = 2; x <= dimensions; x++)
- arraySeparators[x] = separator = ']' + separator + "\\[";
- arrayTypes = new Class[dimensions + 1];
- Class temp = arrayTypes[1] = dataType;
- for(int x = 2; x <= dimensions; x++)
- arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
- str = str.substring(dimensions, str.length() - dimensions);
- Object r = createArrayRecursive(str, dimensions);
- arraySeparators = null;
- arrayTypes = null;
- return r;
- }
- private static Object createArrayRecursive(String str, int dimension){
- if(dimension == 1){
- String[] s = str.split(", ");
- Class dataType = arrayTypes[1];
- Object result = Array.newInstance(dataType, s.length);
- for(int x = 0; x < s.length; x++){
- if(dataType == String.class) ((String[])result)[x] = s[x];
- else if(dataType == int.class) ((int[])result)[x] = Integer.parseInt(s[x]);
- else if(dataType == double.class) ((double[])result)[x] = Double.parseDouble(s[x]);
- else if(dataType == float.class) ((float[])result)[x] = Float.parseFloat(s[x]);
- else if(dataType == long.class) ((long[])result)[x] = Long.parseLong(s[x]);
- else if(dataType == boolean.class) ((boolean[])result)[x] = Boolean.parseBoolean(s[x]);
- else if(dataType == short.class) ((short[])result)[x] = Short.parseShort(s[x]);
- else if(dataType == byte.class) ((byte[])result)[x] = Byte.parseByte(s[x]);
- else if(dataType == char.class) ((char[])result)[x] = s[x].charAt(0);
- }
- return result;
- }
- String[] s = str.split(arraySeparators[dimension]);
- Object arr = Array.newInstance(arrayTypes[dimension], s.length);
- dimension--;
- for(int x = 0; x < s.length; x++)
- Array.set(arr, x, createArrayRecursive(s[x], dimension));
- return arr;
- }
- public static <T> Object reverseDeepToString2(String str, Class<T> dataType){
- int dimensions = 0;
- while(str.charAt(dimensions) == '[')
- dimensions++;
- arraySeparators = new String[dimensions + 1];
- String separator = ", ";
- for(int x = 2; x <= dimensions; x++)
- arraySeparators[x] = separator = ']' + separator + "\\[";
- arrayTypes = new Class[dimensions + 1];
- Class temp = arrayTypes[1] = dataType;
- for(int x = 2; x <= dimensions; x++)
- arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
- str = str.substring(dimensions, str.length() - dimensions);
- Object r = createArrayRecursive2(str, dimensions);
- arraySeparators = null;
- arrayTypes = null;
- return r;
- }
- private static Object createArrayRecursive2(String str, int dimension){
- if(dimension == 1){
- String[] s = str.split(", ");
- Class dataType = arrayTypes[1];
- Object result = Array.newInstance(dataType, s.length);
- for(int x = 0; x < s.length; x++){
- if(dataType == String.class) Array.set(result, x, s[x]);
- else if(dataType == int.class) Array.setInt(result, x, Integer.parseInt(s[x]));
- else if(dataType == double.class) Array.setDouble(result, x, Double.parseDouble(s[x]));
- else if(dataType == float.class) Array.setFloat(result, x, Float.parseFloat(s[x]));
- else if(dataType == long.class) Array.setLong(result, x, Long.parseLong(s[x]));
- else if(dataType == boolean.class) Array.setBoolean(result, x, Boolean.parseBoolean(s[x]));
- else if(dataType == short.class) Array.setShort(result, x, Short.parseShort(s[x]));
- else if(dataType == byte.class) Array.setByte(result, x, Byte.parseByte(s[x]));
- else if(dataType == char.class) Array.setChar(result, x, s[x].charAt(0));
- }
- return result;
- }
- String[] s = str.split(arraySeparators[dimension]);
- Object arr = Array.newInstance(arrayTypes[dimension], s.length);
- dimension--;
- for(int x = 0; x < s.length; x++)
- Array.set(arr, x, createArrayRecursive2(s[x], dimension));
- return arr;
- }
- public static <T> Object reverseDeepToString3(String str, Class<T> dataType){
- int dimensions = 0;
- while(str.charAt(dimensions) == '[')
- dimensions++;
- arraySeparators = new String[dimensions];
- String separator = ", ";
- for(int x = 1; x < dimensions; x++)
- arraySeparators[x] = separator = ']' + separator + "\\[";
- arrayTypes = new Class[dimensions];
- Class temp = arrayTypes[0] = dataType;
- for(int x = 1; x < dimensions; x++)
- arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
- str = str.substring(dimensions, str.length() - dimensions);
- Object r = createArrayRecursive3(str, dimensions - 1);
- arraySeparators = null;
- arrayTypes = null;
- return r;
- }
- private static Object createArrayRecursive3(String str, int dimension){
- if(dimension == 0){
- String[] s = str.split(", ");
- Class dataType = arrayTypes[0];
- if(dataType == String.class){
- String[] result = new String[s.length];
- for(int x = 0; x < s.length; x++) result[x] = s[x];
- return result;}
- if(dataType == int.class){
- int[] result = new int[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Integer.parseInt(s[x]);
- return result;}
- if(dataType == double.class){
- double[] result = new double[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Double.parseDouble(s[x]);
- return result;}
- if(dataType == float.class){
- float[] result = new float[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Float.parseFloat(s[x]);
- return result;}
- if(dataType == long.class){
- long[] result = new long[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Long.parseLong(s[x]);
- return result;}
- if(dataType == boolean.class){
- boolean[] result = new boolean[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Boolean.parseBoolean(s[x]);
- return result;}
- if(dataType == short.class){
- short[] result = new short[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Short.parseShort(s[x]);
- return result;}
- if(dataType == byte.class){
- byte[] result = new byte[s.length];
- for(int x = 0; x < s.length; x++) result[x] = Byte.parseByte(s[x]);
- return result;}
- if(dataType == char.class){
- char[] result = new char[s.length];
- for(int x = 0; x < s.length; x++) result[x] = s[x].charAt(0);
- return result;}
- }
- String[] s = str.split(arraySeparators[dimension]);
- Object arr = Array.newInstance(arrayTypes[dimension], s.length);
- dimension--;
- for(int x = 0; x < s.length; x++)
- Array.set(arr, x, createArrayRecursive3(s[x], dimension));
- return arr;
- }
- public static Object reverseDeepToString_non_generic(String str){
- int dimensions = 0;
- while(str.charAt(dimensions) == '[')
- dimensions++;
- arraySeparators = new String[dimensions];
- String separator = ", ";
- for(int x = 1; x < dimensions; x++)
- arraySeparators[x] = separator = ']' + separator + "\\[";
- arrayTypes = new Class[dimensions];
- Class temp = double.class;
- for(int x = 1; x < dimensions; x++)
- arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
- str = str.substring(dimensions, str.length() - dimensions);
- Object r = createArrayRecursive_non_generic(str, dimensions - 1);
- arraySeparators = null;
- arrayTypes = null;
- return r;
- }
- private static Object createArrayRecursive_non_generic(String str, int dimension){
- if(dimension == 0){
- String[] s = str.split(", ");
- double[] result = new double[s.length];
- for(int x = 0; x < s.length; x++)
- result[x] = Double.parseDouble(s[x]);
- return result;
- }
- String[] s = str.split(arraySeparators[dimension]);
- Object arr = Array.newInstance(arrayTypes[dimension], s.length);
- for(int x = 0; x < s.length; x++)
- Array.set(arr, x, createArrayRecursive_non_generic(s[x], dimension - 1));
- return arr;
- }
- /*
- f1 -> reverseDeepToString(...) - the function with: ((double[])result)[x] = Double.parseDouble(s[x]);
- f2 -> reverseDeepToString2(...) - the function with: Array.setDouble(result, x, Double.parseDouble(s[x]));
- Output:
- f1: 2,371,005,000 ns f2: 2,662,486,000 ns difference: 291,481,000 ns 12%
- f1: 1,938,882,000 ns f2: 2,622,559,000 ns difference: 683,677,000 ns 35%
- f1: 1,991,099,000 ns f2: 2,611,823,000 ns difference: 620,724,000 ns 31%
- f1: 1,947,954,000 ns f2: 2,594,235,000 ns difference: 646,281,000 ns 33%
- f1: 2,005,851,000 ns f2: 2,654,572,000 ns difference: 648,721,000 ns 32%
- f1: 1,993,369,000 ns f2: 2,643,300,000 ns difference: 649,931,000 ns 32%
- f1: 2,027,522,000 ns f2: 2,565,658,000 ns difference: 538,136,000 ns 26%
- f1: 1,896,093,000 ns f2: 2,768,087,000 ns difference: 871,994,000 ns 45%
- f1: 2,022,638,000 ns f2: 2,630,540,000 ns difference: 607,902,000 ns 30%
- f1: 1,971,228,000 ns f2: 2,752,922,000 ns difference: 781,694,000 ns 39%
- Average:
- f1: 2,016,564,100 ns f2: 2,650,618,200 ns difference: 634,054,100 ns 31.44%
- Conclusion: casting is faster than using Array.setDouble(...)
- f1 -> reverseDeepToString(...) - the function with: ((double[])result)[x] = Double.parseDouble(s[x]);
- f2 -> reverseDeepToString3(...) - the function with: Array.setDouble(result, x, Double.parseDouble(s[x]));
- f1: 2,752,147,000 ns f2: 2,416,730,000 ns difference: -335,417,000 ns -12%
- f1: 2,389,684,000 ns f2: 2,350,075,000 ns difference: -39,609,000 ns -1%
- f1: 2,445,088,000 ns f2: 2,145,846,000 ns difference: -299,242,000 ns -12%
- f1: 2,275,553,000 ns f2: 2,110,366,000 ns difference: -165,187,000 ns -7%
- f1: 2,138,603,000 ns f2: 2,076,417,000 ns difference: -62,186,000 ns -2%
- f1: 2,086,091,000 ns f2: 2,101,162,000 ns difference: 15,071,000 ns 0%
- f1: 2,184,546,000 ns f2: 2,034,823,000 ns difference: -149,723,000 ns -6%
- f1: 2,193,979,000 ns f2: 2,212,662,000 ns difference: 18,683,000 ns 0%
- f1: 2,301,970,000 ns f2: 2,096,849,000 ns difference: -205,121,000 ns -8%
- f1: 2,225,511,000 ns f2: 2,102,826,000 ns difference: -122,685,000 ns -5%
- Average:
- f1: 2,299,317,200 ns f2: 2,164,775,600 ns difference: -134,541,600 ns -5.85%
- Conclusion: avoiding casting makes a slight performance improvement
- Results of comparing the generic and the non-generic function:
- f1 -> reverseDeepToString3(...) - generic
- f2 -> reverseDeepToString_non_generic(...) - non-generic
- f1: 2,366,078,000 ns f2: 2,124,151,000 ns difference: -241,927,000 ns -10%
- f1: 2,029,811,000 ns f2: 2,077,671,000 ns difference: 47,860,000 ns 2%
- f1: 2,046,568,000 ns f2: 2,056,158,000 ns difference: 9,590,000 ns 0%
- f1: 2,045,949,000 ns f2: 2,122,674,000 ns difference: 76,725,000 ns 3%
- f1: 2,049,353,000 ns f2: 2,014,930,000 ns difference: -34,423,000 ns -1%
- f1: 2,027,440,000 ns f2: 2,027,194,000 ns difference: -246,000 ns 0%
- f1: 2,047,661,000 ns f2: 2,049,836,000 ns difference: 2,175,000 ns 0%
- f1: 2,080,820,000 ns f2: 2,068,804,000 ns difference: -12,016,000 ns 0%
- f1: 2,021,420,000 ns f2: 2,036,567,000 ns difference: 15,147,000 ns 0%
- f1: 2,107,124,000 ns f2: 2,010,394,000 ns difference: -96,730,000 ns -4%
- Average:
- f1: 2,082,222,400 ns f2: 2,058,837,900 ns difference: -23,384,500 ns -1.12%
- Conclusion: a generic method can be about as fast as a non-generic method.
- */
Add Comment
Please, Sign In to add comment