Advertisement
tmax

Prueba Caché

Dec 10th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.andres.test;
  2.  
  3. public class Main
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         int[][] array = new int[10000][10000];
  8.         long start, end;
  9.        
  10.         System.out.println("Columnas: " + array.length);
  11.         System.out.println("Filas: " + array[0].length);
  12.        
  13.         /*
  14.          * Primera prueba, for con número fijo y escaneo por elementos de fila
  15.          */
  16.         start = System.nanoTime();
  17.         for(int i = 0; i < 10000; ++i){
  18.             for(int j = 0; j < 10000; ++j){
  19.                 array[i][j] = i+j;
  20.             }  
  21.         }
  22.         end = System.nanoTime();
  23.         System.out.println("Prueba 1: " + (end-start)/1000000 + " mS");
  24.        
  25.         /*
  26.          * Segunda prueba, for con número fijo y escaneo por elementos de columna
  27.          */
  28.         start = System.nanoTime();
  29.         for(int j = 0; j < 10000; ++j){
  30.             for(int i = 0; i < 10000; ++i){
  31.                 array[i][j] = i+j;
  32.             }  
  33.         }
  34.         end = System.nanoTime();
  35.         System.out.println("Prueba 2: " + (end-start)/1000000 + " mS");
  36.        
  37.         /*
  38.          * Tercara prueba, for con longitud de array y escaneo por elementos de fila
  39.          */
  40.         start = System.nanoTime();
  41.         for(int i = 0; i < array[0].length; ++i){
  42.             for(int j = 0; j < array.length; ++j){
  43.                 array[i][j] = i+j;
  44.             }  
  45.         }
  46.         end = System.nanoTime();
  47.         System.out.println("Prueba 3: " + (end-start)/1000000 + " mS");
  48.        
  49.         /*
  50.          * Cuarta prueba, for con longitud de array y escaneo por elementos de columna
  51.          */
  52.         start = System.nanoTime();
  53.         for(int j = 0; j < array.length; ++j){
  54.             for(int i = 0; i < array[0].length; ++i){
  55.                 array[i][j] = i+j;
  56.             }  
  57.         }
  58.         end = System.nanoTime();
  59.         System.out.println("Prueba 4: " + (end-start)/1000000 + " mS");
  60.        
  61.         /**
  62.          * Conclusión: iterar sobre el segundo elemento del array (toda la fila) antes de pasar a la siguiente fila
  63.          * es muchisimo más rapido porque en caché se guarda toda la fila de la matriz entonces no se
  64.          * necesita el acceso a RAM hasta que se pase a la siguiente:
  65.          *
  66.          *       1      2       3       4       5
  67.          *       ^  ^       ^       ^       ^
  68.          *       |      |       |       |       |
  69.          *      |1  2   3   4   5|
  70.          *      |6  7   8   9   1|
  71.          *      |2  3   4   5   6|
  72.          *      |7  8   9   0   1|
  73.          *
  74.          * Por ejemplo, se accede más rapido accediendo a los número 1,2,3,4,5 (toda la primera fila) porque en caché
  75.          * se almacena toda la fila de forma contigua. Si accediera por los elementos de la columna, estaría cambiando
  76.          * de fila cada vez que accedo a un nuevo número, demorando debido al tiempo de acceso de RAM y el tiempo de
  77.          * fetch de la nueva fila al cache.
  78.          * Es decir, debemos iterar sobre el segundo elemento de un array 2D [][] todo lo posible antes de cambiar
  79.          * el segundo elemento para aprovechar los datos en caché. En un array de 10000 este método es 32 veces más rapido.
  80.          *
  81.          *        Columna   Fila            El primer elemento del array 2D representa la columna y el segundo la fila.
  82.          *       |       |          Para escanear una fila sería [0][0], [0][1], [0][2], etc.
  83.          *      [C]     [F]
  84.          */
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement