Advertisement
Guest User

Mapped iteration

a guest
Jul 13th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import org.lwjgl.util.mapped.*;
  2.  
  3. public class MappedIterTest {
  4.  
  5.     private MappedIterTest() {
  6.     }
  7.  
  8.     @MappedType(sizeof = 4)
  9.     public static class Mapped extends MappedObject {
  10.  
  11.         public int v;
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         MappedObjectTransformer.register(Mapped.class);
  16.  
  17.         if ( MappedObjectClassLoader.fork(MappedIterTest.class, args) )
  18.             return;
  19.  
  20.         final int SIZE = 100000;
  21.         final int DURATION = 50;
  22.         final int WARMUP = 5 * DURATION;
  23.         final int BENCH = 10 * DURATION;
  24.  
  25.         Mapped foo = Mapped.malloc(SIZE);
  26.  
  27.         benchmarkView(foo, SIZE, WARMUP, BENCH);
  28.         benchmarkView2(foo, SIZE, WARMUP, BENCH);
  29.         benchmarkIter(foo, SIZE, WARMUP, BENCH);
  30.         benchmarkLocal(foo, SIZE, WARMUP, BENCH);
  31.  
  32.         int[] foo2 = new int[SIZE];
  33.         benchmarkJava(foo2, SIZE, WARMUP, BENCH);
  34.     }
  35.  
  36.     private static void benchmarkView(Mapped foo, int SIZE, int WARMUP, int BENCH) {
  37.         int test = 0;
  38.         for ( int i = 0; i < WARMUP; i++ )
  39.             test |= testView(foo, SIZE);
  40.  
  41.         long time = System.nanoTime();
  42.         for ( int i = 0; i < BENCH; i++ )
  43.             test |= testView(foo, SIZE);
  44.         time = (System.nanoTime() - time) / BENCH;
  45.  
  46.         System.out.println("VIEW\n\t" + time + "ns - " + test);
  47.     }
  48.  
  49.     private static void benchmarkView2(Mapped foo, int SIZE, int WARMUP, int BENCH) {
  50.         int test = 0;
  51.         for ( int i = 0; i < WARMUP; i++ )
  52.             test |= testView2(foo, SIZE);
  53.  
  54.         long time = System.nanoTime();
  55.         for ( int i = 0; i < BENCH; i++ )
  56.             test |= testView2(foo, SIZE);
  57.         time = (System.nanoTime() - time) / BENCH;
  58.  
  59.         System.out.println("VIEW2\n\t" + time + "ns - " + test);
  60.     }
  61.  
  62.     private static void benchmarkIter(Mapped foo, int SIZE, int WARMUP, int BENCH) {
  63.         int test = 0;
  64.         for ( int i = 0; i < WARMUP; i++ )
  65.             test |= testIter(foo, SIZE);
  66.  
  67.         long time = System.nanoTime();
  68.         for ( int i = 0; i < BENCH; i++ )
  69.             test |= testIter(foo, SIZE);
  70.         time = (System.nanoTime() - time) / BENCH;
  71.  
  72.         System.out.println("ITER\n\t" + time + "ns - " + test);
  73.     }
  74.  
  75.     private static void benchmarkLocal(Mapped foo, int SIZE, int WARMUP, int BENCH) {
  76.         int test = 0;
  77.         for ( int i = 0; i < WARMUP; i++ )
  78.             test |= testLocal(foo, SIZE);
  79.  
  80.         long time = System.nanoTime();
  81.         for ( int i = 0; i < BENCH; i++ )
  82.             test |= testLocal(foo, SIZE);
  83.         time = (System.nanoTime() - time) / BENCH;
  84.  
  85.         System.out.println("LOCAL\n\t" + time + "ns - " + test);
  86.     }
  87.  
  88.     private static void benchmarkJava(int[] foo, int SIZE, int WARMUP, int BENCH) {
  89.         int test = 0;
  90.         for ( int i = 0; i < WARMUP; i++ )
  91.             test |= testJava(foo, SIZE);
  92.  
  93.         long time = System.nanoTime();
  94.         for ( int i = 0; i < BENCH; i++ )
  95.             test |= testJava(foo, SIZE);
  96.         time = (System.nanoTime() - time) / BENCH;
  97.  
  98.         System.out.println("JAVA\n\t" + time + "ns - " + test);
  99.     }
  100.  
  101.     private static int testView(Mapped foo, int SIZE) {
  102.         int test = 0;
  103.         for ( int j = 0; j < SIZE; j++ ) {
  104.             foo.view = j;
  105.             test |= foo.v;
  106.         }
  107.         return test;
  108.     }
  109.  
  110.     private static int testView2(Mapped foo, int SIZE) {
  111.         int test = 0;
  112.         long viewAddress;
  113.         for ( int j = 0; j < SIZE; j++ ) {
  114.             MappedHelper.put_view(foo, j);
  115.             viewAddress = foo.viewAddress;
  116.             test |= MappedHelper.iget(viewAddress + 0);
  117.         }
  118.         return test;
  119.     }
  120.  
  121.     private static int testIter(Mapped foo, int SIZE) {
  122.         int test = 0;
  123.         for ( Mapped m : MappedObject.foreach(foo, SIZE) ) {
  124.             test |= m.v;
  125.         }
  126.         return test;
  127.     }
  128.  
  129.     private static int testLocal(Mapped foo, int SIZE) {
  130.         int test = 0;
  131.         long viewAddress;
  132.         for ( int j = 0; j < SIZE; j++ ) {
  133.             viewAddress = foo.baseAddress + j * Mapped.SIZEOF;
  134.             test |= MappedHelper.iget(viewAddress + 0);
  135.         }
  136.         return test;
  137.     }
  138.  
  139.     private static int testJava(int[] foo, int SIZE) {
  140.         int test = 0;
  141.         for ( int j = 0; j < SIZE; j++ ) {
  142.             test |= foo[j];
  143.         }
  144.         return test;
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement