MrPolywhirl

MemEval.java

Oct 8th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. package namespace;
  2.  
  3. public class Eval {
  4.    
  5.     public static final int INT, SHORT, BYTE, CHAR, LONG, FLOAT, DOUBLE;
  6.    
  7.     private static final int KB, MB, GB;
  8.     public static final int ARRAY_MIN, ARRAY_MAX;
  9.     public static int[] x;
  10.    
  11.     static {
  12.         INT = Integer.SIZE / Byte.SIZE;
  13.         SHORT = Short.SIZE / Byte.SIZE;
  14.         BYTE = 1;
  15.         CHAR = Character.SIZE / Byte.SIZE;
  16.         LONG = Long.SIZE / Byte.SIZE;
  17.         FLOAT = Float.SIZE / Byte.SIZE;
  18.         DOUBLE = Double.SIZE / Byte.SIZE;
  19.        
  20.         KB = 1024;
  21.         MB = (int) Math.pow(KB, 2);
  22.         GB = MB * KB;
  23.        
  24.         ARRAY_MIN = KB;
  25.         ARRAY_MAX = (int) Math.pow(KB * 4, 2);
  26.         x = new int[ARRAY_MAX];
  27.     }
  28.  
  29.     public static double getSeconds() {
  30.         return System.nanoTime() / 1e9;
  31.     }
  32.  
  33.     public static int label(int i) {       
  34.         if (i < 1e3)
  35.             printf("%1dB,", i);
  36.         else if (i < 1e6)
  37.             printf("%1dK,", i / KB);
  38.         else if (i < 1e9)
  39.             printf("%1dM,", i / MB);
  40.         else
  41.             printf("%1dG,", i / GB);
  42.  
  43.         return 0;
  44.     }
  45.  
  46.     public static void main(String[] args) {
  47.         int nextstep, i, index, stride;
  48.         int csize;
  49.         double steps, tsteps;
  50.         double loadtime, lastsec, sec0, sec1 = 0, sec = 0;
  51.  
  52.         /* Initialize output */
  53.         printf(" ,");
  54.         for (stride = 1; stride <= ARRAY_MAX / 2; stride *= 2)     
  55.             label((int)(stride*sizeof(int.class)));
  56.         printf("\n");
  57.  
  58.         /* Main loop for each configuration */
  59.         for (csize = ARRAY_MIN; csize <= ARRAY_MAX; csize *= 2) {
  60.             label(csize * sizeof(int.class)); /* Print cache size for this loop */
  61.             for (stride = 1; stride <= csize / 2; stride *= 2) {
  62.                 /* Layout path of memory references in array */
  63.                 for (index = 0; index < csize; index += stride)
  64.                     x[index] = index + stride; /* Pointer to next */
  65.                 x[index - stride] = 0;
  66.  
  67.                 /* Wait for timer to roll over */
  68.                 lastsec = getSeconds();
  69.                 do sec0 = getSeconds(); while (sec0 == lastsec);
  70.                
  71.                 /* Walk through path in array for twenty seconds */
  72.                 /* This gives 5% accuracy with second resolution */
  73.                 steps = 0.0; /* Number of steps taken */
  74.                 nextstep = 0; /* Start at beginning of path */
  75.                 sec0 = getSeconds(); /* Start timer */
  76.                 do  { /* Repeat for 20 seconds */
  77.                     for (i=stride; i != 0; i--) { /* Keep sample size */
  78.                          nextstep = 0;
  79.                          do nextstep = x[nextstep];
  80.                          while (nextstep != 0);
  81.                     }
  82.                     steps++;
  83.                     sec1 = getSeconds();
  84.                 } while ((sec1 - sec0) < 20f);
  85.                 sec = sec1 - sec0;
  86.                
  87.                 /* Repeat empty loop to loop subtracted overhead */
  88.                 tsteps = 0.0; /* used to match no. while iterations */
  89.                 sec0 = getSeconds(); /* Start timer */
  90.                 do {
  91.                     for (i=stride; i != 0; i--) { /* Keep sample size */
  92.                         index = 0;
  93.                         do index += stride;
  94.                         while (index < csize);
  95.                     }
  96.                     tsteps++;
  97.                     sec1 = getSeconds();
  98.                 } while (tsteps < steps);
  99.                 sec -= (sec1 - sec0);
  100.                 loadtime = (sec * 1e9) / (steps * csize);
  101.                
  102.                 /* Write out results in CSV format */
  103.                 printf("%4.1f,", loadtime < 0.1 ? 0.1 : loadtime);
  104.             } /* End of inner loop */
  105.             System.out.println();
  106.         } /* End of outer loop */
  107.     }
  108.  
  109.     /** Utility function to emulate c functionality */
  110.    
  111.     public static void printf(String format, Object... args) {
  112.         System.out.printf(format, args);
  113.     }
  114.    
  115.     public static int sizeof(Class<?> dataType) {
  116.         if (dataType == null)
  117.             throw new NullPointerException();
  118.        
  119.         if (dataType == int.class    || dataType == Integer.class)   return INT;
  120.         if (dataType == short.class  || dataType == Short.class)     return SHORT;
  121.         if (dataType == byte.class   || dataType == Byte.class)      return BYTE;
  122.         if (dataType == char.class   || dataType == Character.class) return CHAR;
  123.         if (dataType == long.class   || dataType == Long.class)      return LONG;
  124.         if (dataType == float.class  || dataType == Float.class)     return FLOAT;
  125.         if (dataType == double.class || dataType == Double.class)    return DOUBLE;
  126.  
  127.         return INT;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment