Guest User

array setter speed test

a guest
Oct 23rd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.77 KB | None | 0 0
  1.     public static void main(String args[]){
  2.         ThreadMXBean bean = ManagementFactory.getThreadMXBean();
  3.         DecimalFormat formatter = new DecimalFormat("#,###");
  4.         DecimalFormat formatter2 = new DecimalFormat("#,###.##");
  5.         long[] log = new long[20];
  6.         long startTime, finishTime, mem1, mem2;
  7.        
  8.         for(int r = 0; r < 10; r++){
  9.             startTime = bean.getCurrentThreadCpuTime();
  10.  
  11.             for(int x = 0; x < 1; x++)
  12.                 function1();
  13.            
  14.             finishTime = bean.getCurrentThreadCpuTime();
  15.             mem1 = (finishTime - startTime);
  16.             startTime = bean.getCurrentThreadCpuTime();
  17.  
  18.             for(int x = 0; x < 1; x++)
  19.                 function2();
  20.            
  21.             finishTime = bean.getCurrentThreadCpuTime();
  22.             mem2 = (finishTime - startTime);
  23.            
  24.             log[r*2] = mem1;
  25.             log[r*2+1] = mem2;
  26.            
  27.             System.out.print("f1: " + formatter.format(mem1) + " ns\t");
  28.             System.out.print("f2: " + formatter.format(mem2) + " ns\t");
  29.             System.out.print("difference: " + formatter.format(mem2 - mem1) + " ns\t");
  30.             System.out.println((int)((double)mem2 * 100 / (double)mem1 - 100) + "%");
  31.         }
  32.        
  33.         System.out.println("Average:");
  34.         mem1 = 0;
  35.         for(int x = 0; x < 20; x+=2)
  36.             mem1 += log[x];
  37.         System.out.print("f1: " + formatter.format(mem1 / 10) + " ns\t");
  38.        
  39.         mem2 = 0;
  40.         for(int x = 1; x < 20; x+=2)
  41.             mem2 += log[x];
  42.         System.out.print("f2: " + formatter.format(mem2 / 10) + " ns\t");
  43.        
  44.         System.out.print("difference: " + formatter.format((mem2 - mem1) / 10) + " ns\t");
  45.         System.out.println(formatter2.format((double)mem2 * 100 / (double)mem1 - 100) + "%");
  46.     }
  47.    
  48.     private static String str = Arrays.deepToString(new double[10][20][30][40][50]);
  49.    
  50.     private static void function1(){
  51.         reverseDeepToString(str, double.class);
  52.     }
  53.    
  54.     private static void function2(){
  55.         reverseDeepToString2(str, double.class);
  56.     }
  57.    
  58.    
  59.    
  60.    
  61.    
  62.    
  63.    
  64.    
  65.     private static String[] arraySeparators;
  66.     private static Class[] arrayTypes;
  67.    
  68.     public static <T> Object reverseDeepToString(String str, Class<T> dataType){
  69.         int dimensions = 0;
  70.         while(str.charAt(dimensions) == '[')
  71.             dimensions++;
  72.        
  73.         arraySeparators = new String[dimensions + 1];
  74.         String separator = ", ";
  75.         for(int x = 2; x <= dimensions; x++)
  76.             arraySeparators[x] = separator = ']' + separator + "\\[";
  77.        
  78.         arrayTypes = new Class[dimensions + 1];
  79.         Class temp = arrayTypes[1] = dataType;
  80.         for(int x = 2; x <= dimensions; x++)
  81.             arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
  82.        
  83.         str = str.substring(dimensions, str.length() - dimensions);
  84.         Object r = createArrayRecursive(str, dimensions);
  85.        
  86.         arraySeparators = null;
  87.         arrayTypes = null;
  88.         return r;
  89.     }
  90.    
  91.     private static Object createArrayRecursive(String str, int dimension){
  92.        
  93.         if(dimension == 1){
  94.             String[] s = str.split(", ");
  95.             Class dataType = arrayTypes[1];
  96.             Object result = Array.newInstance(dataType, s.length);
  97.             for(int x = 0; x < s.length; x++){
  98.                 if(dataType == String.class) ((String[])result)[x] = s[x];
  99.                 else if(dataType == int.class) ((int[])result)[x] = Integer.parseInt(s[x]);
  100.                 else if(dataType == double.class) ((double[])result)[x] = Double.parseDouble(s[x]);
  101.                 else if(dataType == float.class) ((float[])result)[x] = Float.parseFloat(s[x]);
  102.                 else if(dataType == long.class) ((long[])result)[x] = Long.parseLong(s[x]);
  103.                 else if(dataType == boolean.class) ((boolean[])result)[x] = Boolean.parseBoolean(s[x]);
  104.                 else if(dataType == short.class) ((short[])result)[x] = Short.parseShort(s[x]);
  105.                 else if(dataType == byte.class) ((byte[])result)[x] = Byte.parseByte(s[x]);
  106.                 else if(dataType == char.class) ((char[])result)[x] = s[x].charAt(0);
  107.             }
  108.             return result;
  109.         }
  110.         String[] s = str.split(arraySeparators[dimension]);
  111.         Object arr = Array.newInstance(arrayTypes[dimension], s.length);
  112.        
  113.         dimension--;
  114.         for(int x = 0; x < s.length; x++)
  115.             Array.set(arr, x, createArrayRecursive(s[x], dimension));
  116.        
  117.         return arr;
  118.     }
  119.    
  120.    
  121.    
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.    
  130.    
  131.    
  132.    
  133.    
  134.    
  135.     public static <T> Object reverseDeepToString2(String str, Class<T> dataType){
  136.         int dimensions = 0;
  137.         while(str.charAt(dimensions) == '[')
  138.             dimensions++;
  139.  
  140.         arraySeparators = new String[dimensions + 1];
  141.         String separator = ", ";
  142.         for(int x = 2; x <= dimensions; x++)
  143.             arraySeparators[x] = separator = ']' + separator + "\\[";
  144.  
  145.         arrayTypes = new Class[dimensions + 1];
  146.         Class temp = arrayTypes[1] = dataType;
  147.         for(int x = 2; x <= dimensions; x++)
  148.             arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
  149.  
  150.         str = str.substring(dimensions, str.length() - dimensions);
  151.         Object r = createArrayRecursive2(str, dimensions);
  152.  
  153.         arraySeparators = null;
  154.         arrayTypes = null;
  155.         return r;
  156.     }
  157.  
  158.     private static Object createArrayRecursive2(String str, int dimension){
  159.  
  160.         if(dimension == 1){
  161.             String[] s = str.split(", ");
  162.             Class dataType = arrayTypes[1];
  163.             Object result = Array.newInstance(dataType, s.length);
  164.             for(int x = 0; x < s.length; x++){
  165.                 if(dataType == String.class) Array.set(result, x, s[x]);
  166.                 else if(dataType == int.class) Array.setInt(result, x, Integer.parseInt(s[x]));
  167.                 else if(dataType == double.class) Array.setDouble(result, x, Double.parseDouble(s[x]));
  168.                 else if(dataType == float.class) Array.setFloat(result, x, Float.parseFloat(s[x]));
  169.                 else if(dataType == long.class) Array.setLong(result, x, Long.parseLong(s[x]));
  170.                 else if(dataType == boolean.class) Array.setBoolean(result, x, Boolean.parseBoolean(s[x]));
  171.                 else if(dataType == short.class) Array.setShort(result, x, Short.parseShort(s[x]));
  172.                 else if(dataType == byte.class) Array.setByte(result, x, Byte.parseByte(s[x]));
  173.                 else if(dataType == char.class) Array.setChar(result, x, s[x].charAt(0));
  174.             }
  175.             return result;
  176.         }
  177.         String[] s = str.split(arraySeparators[dimension]);
  178.         Object arr = Array.newInstance(arrayTypes[dimension], s.length);
  179.  
  180.         dimension--;
  181.         for(int x = 0; x < s.length; x++)
  182.             Array.set(arr, x, createArrayRecursive2(s[x], dimension));
  183.  
  184.         return arr;
  185.     }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.     public static <T> Object reverseDeepToString3(String str, Class<T> dataType){
  196.         int dimensions = 0;
  197.         while(str.charAt(dimensions) == '[')
  198.             dimensions++;
  199.  
  200.         arraySeparators = new String[dimensions];
  201.         String separator = ", ";
  202.         for(int x = 1; x < dimensions; x++)
  203.             arraySeparators[x] = separator = ']' + separator + "\\[";
  204.  
  205.         arrayTypes = new Class[dimensions];
  206.         Class temp = arrayTypes[0] = dataType;
  207.         for(int x = 1; x < dimensions; x++)
  208.             arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
  209.  
  210.         str = str.substring(dimensions, str.length() - dimensions);
  211.         Object r = createArrayRecursive3(str, dimensions - 1);
  212.  
  213.         arraySeparators = null;
  214.         arrayTypes = null;
  215.         return r;
  216.     }
  217.    
  218.     private static Object createArrayRecursive3(String str, int dimension){
  219.  
  220.         if(dimension == 0){
  221.             String[] s = str.split(", ");
  222.             Class dataType = arrayTypes[0];
  223.            
  224.             if(dataType == String.class){
  225.                 String[] result = new String[s.length];
  226.                 for(int x = 0; x < s.length; x++) result[x] = s[x];
  227.                 return result;}
  228.             if(dataType == int.class){
  229.                 int[] result = new int[s.length];
  230.                 for(int x = 0; x < s.length; x++) result[x] = Integer.parseInt(s[x]);
  231.                 return result;}
  232.             if(dataType == double.class){
  233.                 double[] result = new double[s.length];
  234.                 for(int x = 0; x < s.length; x++) result[x] = Double.parseDouble(s[x]);
  235.                 return result;}
  236.             if(dataType == float.class){
  237.                 float[] result = new float[s.length];
  238.                 for(int x = 0; x < s.length; x++) result[x] = Float.parseFloat(s[x]);
  239.                 return result;}
  240.             if(dataType == long.class){
  241.                 long[] result = new long[s.length];
  242.                 for(int x = 0; x < s.length; x++) result[x] = Long.parseLong(s[x]);
  243.                 return result;}
  244.             if(dataType == boolean.class){
  245.                 boolean[] result = new boolean[s.length];
  246.                 for(int x = 0; x < s.length; x++) result[x] = Boolean.parseBoolean(s[x]);
  247.                 return result;}
  248.             if(dataType == short.class){
  249.                 short[] result = new short[s.length];
  250.                 for(int x = 0; x < s.length; x++) result[x] = Short.parseShort(s[x]);
  251.                 return result;}
  252.             if(dataType == byte.class){
  253.                 byte[] result = new byte[s.length];
  254.                 for(int x = 0; x < s.length; x++) result[x] = Byte.parseByte(s[x]);
  255.                 return result;}
  256.             if(dataType == char.class){
  257.                 char[] result = new char[s.length];
  258.                 for(int x = 0; x < s.length; x++) result[x] = s[x].charAt(0);
  259.                 return result;}
  260.         }
  261.         String[] s = str.split(arraySeparators[dimension]);
  262.         Object arr = Array.newInstance(arrayTypes[dimension], s.length);
  263.  
  264.         dimension--;
  265.         for(int x = 0; x < s.length; x++)
  266.             Array.set(arr, x, createArrayRecursive3(s[x], dimension));
  267.  
  268.         return arr;
  269.     }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.     public static Object reverseDeepToString_non_generic(String str){
  284.         int dimensions = 0;
  285.         while(str.charAt(dimensions) == '[')
  286.             dimensions++;
  287.  
  288.         arraySeparators = new String[dimensions];
  289.         String separator = ", ";
  290.         for(int x = 1; x < dimensions; x++)
  291.             arraySeparators[x] = separator = ']' + separator + "\\[";
  292.  
  293.         arrayTypes = new Class[dimensions];
  294.         Class temp = double.class;
  295.         for(int x = 1; x < dimensions; x++)
  296.             arrayTypes[x] = temp = Array.newInstance(temp, 0).getClass();
  297.  
  298.         str = str.substring(dimensions, str.length() - dimensions);
  299.         Object r = createArrayRecursive_non_generic(str, dimensions - 1);
  300.  
  301.         arraySeparators = null;
  302.         arrayTypes = null;
  303.         return r;
  304.     }
  305.  
  306.     private static Object createArrayRecursive_non_generic(String str, int dimension){
  307.  
  308.         if(dimension == 0){
  309.             String[] s = str.split(", ");
  310.             double[] result = new double[s.length];
  311.             for(int x = 0; x < s.length; x++)
  312.                 result[x] = Double.parseDouble(s[x]);
  313.             return result;
  314.         }
  315.         String[] s = str.split(arraySeparators[dimension]);
  316.         Object arr = Array.newInstance(arrayTypes[dimension], s.length);
  317.  
  318.         for(int x = 0; x < s.length; x++)
  319.             Array.set(arr, x, createArrayRecursive_non_generic(s[x], dimension - 1));
  320.  
  321.         return arr;
  322.     }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. /*
  330. f1 -> reverseDeepToString(...) - the function with: ((double[])result)[x] = Double.parseDouble(s[x]);
  331. f2 -> reverseDeepToString2(...) - the function with: Array.setDouble(result, x, Double.parseDouble(s[x]));
  332. Output:
  333.  
  334. f1: 2,371,005,000 ns    f2: 2,662,486,000 ns    difference: 291,481,000 ns  12%
  335. f1: 1,938,882,000 ns    f2: 2,622,559,000 ns    difference: 683,677,000 ns  35%
  336. f1: 1,991,099,000 ns    f2: 2,611,823,000 ns    difference: 620,724,000 ns  31%
  337. f1: 1,947,954,000 ns    f2: 2,594,235,000 ns    difference: 646,281,000 ns  33%
  338. f1: 2,005,851,000 ns    f2: 2,654,572,000 ns    difference: 648,721,000 ns  32%
  339. f1: 1,993,369,000 ns    f2: 2,643,300,000 ns    difference: 649,931,000 ns  32%
  340. f1: 2,027,522,000 ns    f2: 2,565,658,000 ns    difference: 538,136,000 ns  26%
  341. f1: 1,896,093,000 ns    f2: 2,768,087,000 ns    difference: 871,994,000 ns  45%
  342. f1: 2,022,638,000 ns    f2: 2,630,540,000 ns    difference: 607,902,000 ns  30%
  343. f1: 1,971,228,000 ns    f2: 2,752,922,000 ns    difference: 781,694,000 ns  39%
  344. Average:
  345. f1: 2,016,564,100 ns    f2: 2,650,618,200 ns    difference: 634,054,100 ns  31.44%
  346.  
  347. Conclusion: casting is faster than using Array.setDouble(...)
  348.  
  349.  
  350.  
  351. f1 -> reverseDeepToString(...) - the function with: ((double[])result)[x] = Double.parseDouble(s[x]);
  352. f2 -> reverseDeepToString3(...) - the function with: Array.setDouble(result, x, Double.parseDouble(s[x]));
  353.  
  354. f1: 2,752,147,000 ns    f2: 2,416,730,000 ns    difference: -335,417,000 ns -12%
  355. f1: 2,389,684,000 ns    f2: 2,350,075,000 ns    difference: -39,609,000 ns  -1%
  356. f1: 2,445,088,000 ns    f2: 2,145,846,000 ns    difference: -299,242,000 ns -12%
  357. f1: 2,275,553,000 ns    f2: 2,110,366,000 ns    difference: -165,187,000 ns -7%
  358. f1: 2,138,603,000 ns    f2: 2,076,417,000 ns    difference: -62,186,000 ns  -2%
  359. f1: 2,086,091,000 ns    f2: 2,101,162,000 ns    difference: 15,071,000 ns   0%
  360. f1: 2,184,546,000 ns    f2: 2,034,823,000 ns    difference: -149,723,000 ns -6%
  361. f1: 2,193,979,000 ns    f2: 2,212,662,000 ns    difference: 18,683,000 ns   0%
  362. f1: 2,301,970,000 ns    f2: 2,096,849,000 ns    difference: -205,121,000 ns -8%
  363. f1: 2,225,511,000 ns    f2: 2,102,826,000 ns    difference: -122,685,000 ns -5%
  364. Average:
  365. f1: 2,299,317,200 ns    f2: 2,164,775,600 ns    difference: -134,541,600 ns -5.85%
  366.  
  367. Conclusion: avoiding casting makes a slight performance improvement
  368.  
  369.  
  370. Results of comparing the generic and the non-generic function:
  371. f1 -> reverseDeepToString3(...) - generic
  372. f2 -> reverseDeepToString_non_generic(...) - non-generic
  373.  
  374. f1: 2,366,078,000 ns    f2: 2,124,151,000 ns    difference: -241,927,000 ns -10%
  375. f1: 2,029,811,000 ns    f2: 2,077,671,000 ns    difference: 47,860,000 ns   2%
  376. f1: 2,046,568,000 ns    f2: 2,056,158,000 ns    difference: 9,590,000 ns    0%
  377. f1: 2,045,949,000 ns    f2: 2,122,674,000 ns    difference: 76,725,000 ns   3%
  378. f1: 2,049,353,000 ns    f2: 2,014,930,000 ns    difference: -34,423,000 ns  -1%
  379. f1: 2,027,440,000 ns    f2: 2,027,194,000 ns    difference: -246,000 ns 0%
  380. f1: 2,047,661,000 ns    f2: 2,049,836,000 ns    difference: 2,175,000 ns    0%
  381. f1: 2,080,820,000 ns    f2: 2,068,804,000 ns    difference: -12,016,000 ns  0%
  382. f1: 2,021,420,000 ns    f2: 2,036,567,000 ns    difference: 15,147,000 ns   0%
  383. f1: 2,107,124,000 ns    f2: 2,010,394,000 ns    difference: -96,730,000 ns  -4%
  384. Average:
  385. f1: 2,082,222,400 ns    f2: 2,058,837,900 ns    difference: -23,384,500 ns  -1.12%
  386.  
  387. Conclusion: a generic method can be about as fast as a non-generic method.
  388. */
Add Comment
Please, Sign In to add comment