Advertisement
Guest User

vz0

a guest
Feb 10th, 2011
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1.  
  2. /*
  3.  
  4. My output:
  5.  
  6. 25037
  7. 13204
  8. 13796
  9.  
  10. */
  11.  
  12. public class MatrixTest {
  13.  
  14.     public int width;
  15.     public int height;
  16.     public int m[];
  17.    
  18.     public MatrixTest(int w, int h) {
  19.         this.width = w;
  20.         this.height = h;
  21.         this.m = new int[w * h];
  22.     }
  23.    
  24.     public int get(int x, int y) {
  25.         return this.m[y * width + x];
  26.     }
  27.    
  28.     public void set(int x, int y, int value) {
  29.         this.m[y * width + x] = value;
  30.     }
  31.    
  32.     public static void main (String[] args) {
  33.         int w = 1000, h = 2000, passes = 400;
  34.        
  35.         int matrix[][] = new int[h][];
  36.        
  37.         for (int i = 0; i < h; ++i) {
  38.             matrix[i] = new int[w];
  39.         }
  40.        
  41.         long start;
  42.        
  43.         start = System.currentTimeMillis();
  44.        
  45.         // X and Y are inverted.
  46.        
  47.         for (int z = 0; z < passes; z++) {
  48.             for (int x = 0; x < w; x++) {
  49.                 for (int y = 0; y < h; y++) {
  50.                     matrix[y][x] = matrix[y][x] + 1;
  51.                 }
  52.             }
  53.         }
  54.        
  55.         System.out.println(System.currentTimeMillis() - start);
  56.        
  57.         //
  58.        
  59.         MatrixTest mt = new MatrixTest(w, h);
  60.        
  61.         start = System.currentTimeMillis();
  62.        
  63.         for (int z = 0; z < passes; z++) {
  64.             for (int x = 0; x < w; x++) {
  65.                 for (int y = 0; y < h; y++) {
  66.                     mt.set(x, y, mt.get(x, y) + 1);
  67.                 }
  68.             }
  69.         }
  70.        
  71.         System.out.println(System.currentTimeMillis() - start);
  72.        
  73.         //
  74.        
  75.         MatrixTest mt2 = new MatrixTest(w, h);
  76.        
  77.         start = System.currentTimeMillis();
  78.        
  79.         for (int z = 0; z < passes; z++) {
  80.             for (int x = 0; x < w; x++) {
  81.                 for (int y = 0; y < h; y++) {
  82.                     mt2.m[y * w + x] = mt2.m[y * w + x] + 1;
  83.                 }
  84.             }
  85.         }
  86.        
  87.         System.out.println(System.currentTimeMillis() - start);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement