Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package namespace;
- public class Eval {
- public static final int INT, SHORT, BYTE, CHAR, LONG, FLOAT, DOUBLE;
- private static final int KB, MB, GB;
- public static final int ARRAY_MIN, ARRAY_MAX;
- public static int[] x;
- static {
- INT = Integer.SIZE / Byte.SIZE;
- SHORT = Short.SIZE / Byte.SIZE;
- BYTE = 1;
- CHAR = Character.SIZE / Byte.SIZE;
- LONG = Long.SIZE / Byte.SIZE;
- FLOAT = Float.SIZE / Byte.SIZE;
- DOUBLE = Double.SIZE / Byte.SIZE;
- KB = 1024;
- MB = (int) Math.pow(KB, 2);
- GB = MB * KB;
- ARRAY_MIN = KB;
- ARRAY_MAX = (int) Math.pow(KB * 4, 2);
- x = new int[ARRAY_MAX];
- }
- public static double getSeconds() {
- return System.nanoTime() / 1e9;
- }
- public static int label(int i) {
- if (i < 1e3)
- printf("%1dB,", i);
- else if (i < 1e6)
- printf("%1dK,", i / KB);
- else if (i < 1e9)
- printf("%1dM,", i / MB);
- else
- printf("%1dG,", i / GB);
- return 0;
- }
- public static void main(String[] args) {
- int nextstep, i, index, stride;
- int csize;
- double steps, tsteps;
- double loadtime, lastsec, sec0, sec1 = 0, sec = 0;
- /* Initialize output */
- printf(" ,");
- for (stride = 1; stride <= ARRAY_MAX / 2; stride *= 2)
- label((int)(stride*sizeof(int.class)));
- printf("\n");
- /* Main loop for each configuration */
- for (csize = ARRAY_MIN; csize <= ARRAY_MAX; csize *= 2) {
- label(csize * sizeof(int.class)); /* Print cache size for this loop */
- for (stride = 1; stride <= csize / 2; stride *= 2) {
- /* Layout path of memory references in array */
- for (index = 0; index < csize; index += stride)
- x[index] = index + stride; /* Pointer to next */
- x[index - stride] = 0;
- /* Wait for timer to roll over */
- lastsec = getSeconds();
- do sec0 = getSeconds(); while (sec0 == lastsec);
- /* Walk through path in array for twenty seconds */
- /* This gives 5% accuracy with second resolution */
- steps = 0.0; /* Number of steps taken */
- nextstep = 0; /* Start at beginning of path */
- sec0 = getSeconds(); /* Start timer */
- do { /* Repeat for 20 seconds */
- for (i=stride; i != 0; i--) { /* Keep sample size */
- nextstep = 0;
- do nextstep = x[nextstep];
- while (nextstep != 0);
- }
- steps++;
- sec1 = getSeconds();
- } while ((sec1 - sec0) < 20f);
- sec = sec1 - sec0;
- /* Repeat empty loop to loop subtracted overhead */
- tsteps = 0.0; /* used to match no. while iterations */
- sec0 = getSeconds(); /* Start timer */
- do {
- for (i=stride; i != 0; i--) { /* Keep sample size */
- index = 0;
- do index += stride;
- while (index < csize);
- }
- tsteps++;
- sec1 = getSeconds();
- } while (tsteps < steps);
- sec -= (sec1 - sec0);
- loadtime = (sec * 1e9) / (steps * csize);
- /* Write out results in CSV format */
- printf("%4.1f,", loadtime < 0.1 ? 0.1 : loadtime);
- } /* End of inner loop */
- System.out.println();
- } /* End of outer loop */
- }
- /** Utility function to emulate c functionality */
- public static void printf(String format, Object... args) {
- System.out.printf(format, args);
- }
- public static int sizeof(Class<?> dataType) {
- if (dataType == null)
- throw new NullPointerException();
- if (dataType == int.class || dataType == Integer.class) return INT;
- if (dataType == short.class || dataType == Short.class) return SHORT;
- if (dataType == byte.class || dataType == Byte.class) return BYTE;
- if (dataType == char.class || dataType == Character.class) return CHAR;
- if (dataType == long.class || dataType == Long.class) return LONG;
- if (dataType == float.class || dataType == Float.class) return FLOAT;
- if (dataType == double.class || dataType == Double.class) return DOUBLE;
- return INT;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment