Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Roquen (posted 2014-05-06 15:32:20, viewed 122 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329  
import static java.lang.System.nanoTime;

public class CacheGrid {
  // ** compile time configurable stuff
  private static final int pow = 10;  // each dim is 2^pow
  private static final int numChunks = 15000;
  private static final int warmRounds = 5;
  private static final int trials = 10000;
  private static final int trialPrintRate = 6;
  private static final int lines = 1000;
  
  // ** derived stuff
  private static final int dim = (1 << pow);
  private static final int size = dim*dim;
  private static final int mask = size-1;

  private static final int trialMask = (1<<trialPrintRate)-1;
  private static final int[] grid = new int[size];
  private static final int[][] rows = new int[dim][dim];

  private static int _seed = 0;

  private static int nextInt()
  {
    _seed = 0xac549d55 * _seed + 0x61c88647;
    
    return _seed;
  }
  
  private static int nextInt(int n)
  {
    return ((nextInt()>>>15) * n) >>> 17;
  }

  private static int flags = 0;
   
   private static String humanTime(float t)
   {
     // ya this is dumb
     String suffix = "ns";
     
     if (t > 1000.f) {
       t *= 1/1000.f;
       suffix = "us";
       if (t > 1000.f) {
         t *= 1/1000.f;
         suffix = "ms";
       }
     }
     
     return t + suffix;
   }
   
   static int trial = 0;
   
   // constant to determine the choice between the two speeds
   private static final float FILTER_P = 0.1f;  // current: if filtered is within 10% of current then use slow
   
   // constant for slow speed adaption
   private static final float FILTER_S = 0x1p-4f; 
   
   // constant for fast speed adaption
   private static final float FILTER_F = 0x1p-2f;  // current: 75% filtered, 25% 
   
   // REALLY POOR CHOICE FOR THIS
   public static float twospeedIIR(float filtered, float current)
   {
     float diff = Math.abs(filtered-current);
       
     if (diff < FILTER_P*filtered)
       return (1-FILTER_S)*filtered + FILTER_S*current;
     
     return(1-FILTER_F)*filtered + FILTER_F*current;
   }
   
   private static float p(String name, long t0, long t1, float f)
   {
     t1 -= t0;
     f   = twospeedIIR(f, t1);
     
     if ((trial & trialMask) == trialMask)
       System.out.println(name + humanTime(t1) + " ~" + humanTime(f));
     
     return f;
   }
   
   private static float p(String name, long t0, long t1, int r, float f)
   {
     t1 -= t0;
     f   = twospeedIIR(f, t1);
     
     if ((trial & trialMask) == trialMask)
       System.out.println(name + humanTime(t1) + " ~" + humanTime(f) + " (" + r + ")");
     
     return f;
   }

   

   public static void main(String[] args) {
      System.out.println(System.getProperty("java.version"));
      System.out.println(System.getProperty("java.vm.version"));
      
      if (args.length != 0) {
        try {
          flags = Integer.parseInt(args[0], 16);
          System.out.println("flags: 0x" + Integer.toHexString(flags));
        } catch(Exception e) { /**/ }
      }
   
      // very poor simulation of non-address first allocation
      java.util.ArrayList<int[]> mem = new java.util.ArrayList<>();
      
      if ((flags & 0x1)!=0) {
        System.out.println("mixing up row");

          for(int i=0; i<numChunks; i++) {
            int[] a = new int[dim];
            mem.add(a);
          }

        for(int i=0; i<dim; i++) {
          int v = nextInt(mem.size());
          rows[i] = mem.remove(v);
        }
      }
      
      if ((flags & 0x2)!=0) {
        System.out.print("warm a little: junk =");
        int r = 0;
        for(int i=0; i<warmRounds; i++) {
          fill_1d(i);
          fill_2d(i);
          fill_1da(i);
          fill_2da(i); 
          fill_1d_random();
          fill_2d_random();
          fill_1d_L(i);
          fill_2d_L(i);
          r += cnt_1d_L();
          r += cnt_2d_L();
        }
        System.out.println(r);
      }

      float f0=0,f1=0,f2=0,f3=0,f4=0,f5=0,f6=0,f7=0,f8=0,f9=0;
      
      Thread thread = Thread.currentThread();
      
      thread.setPriority(Thread.MAX_PRIORITY);
      
      for(int i = 0; i < trials; i++) {
        int seed = (int)nanoTime(); _seed = seed;
        int r;
        if ((trial & trialMask) == trialMask) System.out.println();
        { long t0 = nanoTime(); fill_1d(i);       long t1 = nanoTime(); f0=p("fill_1d:   ", t0,t1, f0); }
        { long t0 = nanoTime(); fill_2d(i);       long t1 = nanoTime(); f1=p("fill_2d:   ", t0,t1, f1); }
        { long t0 = nanoTime(); fill_1da(i);      long t1 = nanoTime(); f2=p("fill_1d(a):", t0,t1, f2); }
        { long t0 = nanoTime(); fill_2da(i);      long t1 = nanoTime(); f3=p("fill_2d(a):", t0,t1, f3); }
        { long t0 = nanoTime(); fill_1d_random(); long t1 = nanoTime(); f4=p("fill_1d_rd:", t0,t1, f4);    _seed = seed; }
        { long t0 = nanoTime(); fill_2d_random(); long t1 = nanoTime(); f5=p("fill_2d_rd:", t0,t1, f5);    _seed = seed; }
        { long t0 = nanoTime(); fill_1d_L(i);     long t1 = nanoTime(); f6=p("fill_1d_L: ", t0,t1, f6);    _seed = seed; }
        { long t0 = nanoTime(); fill_2d_L(i);     long t1 = nanoTime(); f7=p("fill_2d_L: ", t0,t1, f7);    _seed = seed; }
        { long t0 = nanoTime(); r = cnt_1d_L();   long t1 = nanoTime(); f8=p("cnt_1d_L:  ", t0,t1, r, f8); _seed = seed; }
        { long t0 = nanoTime(); r = cnt_2d_L();   long t1 = nanoTime(); f9=p("cnt_2d_L:  ", t0,t1, r, f9); _seed = seed; }
        trial++;
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          //
        }
      }
    }

   public static void fill_1d_random() {
     for(int i=0; i<size; i++) { // (work with the design)
      //for(int x = 0; x < dim; x++) {
         //for(int y = 0; y < dim; y++) {
            int xi = nextInt(dim);//randX[x];
            int yi = nextInt(dim);//randY[y];

            grid[(yi << pow) | xi] = xi^yi;//yi * dim + xi; (insure same computation)
         //}
      //}
     }
   }

   public static void fill_2d_random() {
     for(int i=0; i<size; i++) { // (work with the design)
       //for(int x = 0; x < dim; x++) {
       //for(int y = 0; y < dim; y++) {
        int xi = nextInt(dim);//randX[x];
        int yi = nextInt(dim);//randY[y];
        rows[yi][xi] = xi^yi;//yi * dim + xi;
        //}
        //}
     }
   }

   public static void fill_1d(int val) {
     for(int i = 0; i < size; i++) grid[i] = val;
   }
   
   public static void fill_2d(int val) {
     for(int y = 0; y < dim; y++) {
       int[] d = rows[y];
       for(int x = 0; x < dim; x++) 
         d[x] = val;
     }
   }
   
   public static void fill_1da(int val) {
     java.util.Arrays.fill(grid, val);
   }
   
   public static void fill_2da(int val) {
     for(int y = 0; y < dim; y++) {
       int[] d = rows[y];
       java.util.Arrays.fill(d, val);
     }
   }
   
   public static void fill_1d_L(int val) {
     for(int j=0; j<lines; j++) {
       int    x0 = nextInt(dim);
       int    y0 = nextInt(dim);
       double x1 = nextInt(dim);
       double y1 = nextInt(dim);
       int    step = 1;
       double dx = x1-x0;
       double dy = y1-y0;
       double ax = Math.abs(dx);
       double ay = Math.abs(dy);
       double n  = ax > ay ? ax : ay;
       double in = 1.0/n;
       int    e =  (int)n;

       for(int i=0; i<e; i++) {
         double d = step*in;
         int x = (int)(x0 + d*dx);
         int y = (int)(y0 + d*dy);
         step += 1;
         grid[(y << pow) | x] = val;
       }
     }
   }
   
   public static int cnt_1d_L() {
     int r = 0;
     
     for(int j=0; j<lines; j++) {
       int    x0 = nextInt(dim);
       int    y0 = nextInt(dim);
       double x1 = nextInt(dim);
       double y1 = nextInt(dim);
       int    step = 1;
       double dx = x1-x0;
       double dy = y1-y0;
       double ax = Math.abs(dx);
       double ay = Math.abs(dy);
       double n  = ax > ay ? ax : ay;
       double in = 1.0/n;
       int    e =  (int)n;

       for(int i=0; i<e; i++) {
         double d = step*in;
         int x = (int)(x0 + d*dx);
         int y = (int)(y0 + d*dy);
         step += 1;
         r += grid[(y << pow) | x];
       }
     }
     return r;
   }
   
   public static void fill_2d_L(int val) {
     for(int j=0; j<lines; j++) {
       int    x0 = nextInt(dim);
       int    y0 = nextInt(dim);
       double x1 = nextInt(dim);
       double y1 = nextInt(dim);
       int    step = 1;
       double dx = x1-x0;
       double dy = y1-y0;
       double ax = Math.abs(dx);
       double ay = Math.abs(dy);
       double n  = ax > ay ? ax : ay;
       double in = 1.0/n;
       int    e =  (int)n;

       for(int i=0; i<e; i++) {
         double d = step*in;
         int x = (int)(x0 + d*dx);
         int y = (int)(y0 + d*dy);
         step += 1;
         rows[y][x] = val;
       }
     }
   }
   
   
   public static int cnt_2d_L() {
     int r = 0;
     
     for(int j=0; j<lines; j++) {
       int    x0 = nextInt(dim);
       int    y0 = nextInt(dim);
       double x1 = nextInt(dim);
       double y1 = nextInt(dim);
       int    step = 1;
       double dx = x1-x0;
       double dy = y1-y0;
       double ax = Math.abs(dx);
       double ay = Math.abs(dy);
       double n  = ax > ay ? ax : ay;
       double in = 1.0/n;
       int    e =  (int)n;

       for(int i=0; i<e; i++) {
         double d = step*in;
         int x = (int)(x0 + d*dx);
         int y = (int)(y0 + d*dy);
         step += 1;
         r += rows[y][x];
       }
     }
     return r;
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Ecumene (149 views)
2017-09-30 02:57:34

theagentd (214 views)
2017-09-26 18:23:31

cybrmynd (300 views)
2017-08-02 12:28:51

cybrmynd (288 views)
2017-08-02 12:19:43

cybrmynd (296 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (968 views)
2017-04-27 17:45:51

buddyBro (1094 views)
2017-04-05 03:38:00

CopyableCougar4 (1669 views)
2017-03-24 15:39:42

theagentd (1428 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!