Guest User

array setter speed test

a guest
Oct 16th, 2018
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void main(String args[]){
  2.     long startTime, finishTime, mem1, mem2;
  3.     for(int r = 0; r < 10; r++){
  4.         startTime = System.currentTimeMillis();
  5.             for(int x = 0; x < 100000000; x++)
  6.             Array.set(arr, 7, 42);
  7.        
  8.         finishTime = System.currentTimeMillis();
  9.         mem1 = (finishTime - startTime);
  10.         startTime = System.currentTimeMillis();
  11.             for(int x = 0; x < 100000000; x++)
  12.             ((int[])arr)[7] = 42;
  13.        
  14.         finishTime = System.currentTimeMillis();
  15.         mem2 = (finishTime - startTime);
  16.        
  17.         System.out.print("Array.set: " + mem1 + " ms\t");
  18.         System.out.println("arr[x]=y: " + mem2 + " ms");
  19.     }
  20. }
  21.  
  22. private static Object arr = new int[10];
  23.  
  24.  
  25. // output:
  26. //
  27. // Array.set: 9785 ms   arr[x]=y: 5 ms
  28. // Array.set: 9273 ms   arr[x]=y: 2 ms
  29. // Array.set: 9135 ms   arr[x]=y: 2 ms
  30. // Array.set: 9436 ms   arr[x]=y: 2 ms
  31. // Array.set: 9896 ms   arr[x]=y: 2 ms
  32. // Array.set: 9546 ms   arr[x]=y: 2 ms
  33. // Array.set: 10011 ms  arr[x]=y: 2 ms
  34. // Array.set: 9643 ms   arr[x]=y: 3 ms
  35. // Array.set: 9630 ms   arr[x]=y: 2 ms
RAW Paste Data