Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 22nd, 2011  |  syntax: Java  |  size: 3.04 KB  |  views: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Since we will be using the large array, or List, to be doing millions of lookups, we want to
  3.  * test if doing lookups on a List<Long> is slower than doing the exact same lookups on a
  4.  * long[].  Unfortunately, it is.
  5.  *
  6.  * Sample output showing 2x speed decrease in memory lookups from gauva wrapped
  7.  * long[] vs a naked primitive long[].  Trove performance, on the other hand, is still
  8.  * very fast, presumably because it avoids boxing/unboxing:
  9.  *
  10.  *  8607317423706504585
  11.  *  154.0
  12.  *  8607317423706504585
  13.  *  337.0
  14.  *  8607317423706504585
  15.  *  156.0
  16.  */
  17. package memory;
  18.  
  19. import java.security.SecureRandom;
  20. import java.util.List;
  21. import gnu.trove.list.array.TLongArrayList;
  22.  
  23. import com.google.common.primitives.Longs;
  24.  
  25. public class MemoryAccessTest {
  26.    
  27.     public static final int NUM_LOOKUPS = 10000000; // number of lookups we'll do
  28.     public static final int ARR_LENGTH = 3000000; // length of array (or List) we'll be performing the lookups on
  29.    
  30.     // a large sample of random lookup indices.
  31.     public static int[] lookupIndices = new int[NUM_LOOKUPS];
  32.     // a random number generator
  33.     public static SecureRandom rng;
  34.    
  35.     static {
  36.         rng = new SecureRandom();
  37.         for (int i=0; i<NUM_LOOKUPS; i++) {
  38.             lookupIndices[i] = rng.nextInt(ARR_LENGTH);
  39.         }
  40.     }
  41.    
  42.     public static void main(String[] args) {
  43.        
  44.         SecureRandom rng = new SecureRandom();
  45.        
  46.         long[] primitive = new long[ARR_LENGTH];
  47.         for (int i=0; i<ARR_LENGTH; i++) {
  48.             primitive[i] = rng.nextLong();
  49.         }
  50.        
  51.         List<Long> guava = Longs.asList(primitive);
  52.         TLongArrayList trove = new TLongArrayList(primitive);
  53.        
  54.         // Time the primitive version
  55.         double startPrimitive = System.currentTimeMillis();
  56.         // a dummy variable for tracking the lookups, just to make
  57.         // sure Hotspot doesn't optimize anything away.
  58.         long sum = 0;
  59.         for (int i=0; i<NUM_LOOKUPS; i++) {
  60.             sum += primitive[lookupIndices[i]];
  61.         }
  62.         double endPrimitive = System.currentTimeMillis();
  63.         System.out.println(sum);
  64.         System.out.println(endPrimitive - startPrimitive);
  65.        
  66.         // Time the guava version
  67.         double startGuava = System.currentTimeMillis();
  68.         sum = 0;
  69.         for (int i=0; i<NUM_LOOKUPS; i++) {
  70.             sum += guava.get(lookupIndices[i]);
  71.         }
  72.         double endGuava = System.currentTimeMillis();
  73.         System.out.println(sum); // sanity check: make sure the two sums match
  74.         System.out.println(endGuava - startGuava);
  75.        
  76.         // Time the trove version
  77.         double startTrove = System.currentTimeMillis();
  78.         sum = 0;
  79.         for (int i=0; i<NUM_LOOKUPS; i++) {
  80.             sum += trove.get(lookupIndices[i]);
  81.         }
  82.         double endTrove = System.currentTimeMillis();
  83.         System.out.println(sum); // sanity check: make sure the two sums match
  84.         System.out.println(endTrove - startTrove);
  85.        
  86.     }
  87. }
clone this paste RAW Paste Data