Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/10/2011, 13:02
Avatar de DeeR
DeeR
 
Fecha de Ingreso: diciembre-2003
Ubicación: Santiago
Mensajes: 520
Antigüedad: 20 años, 4 meses
Puntos: 17
Respuesta: Medir tiempo de Algoritmo

el_shinito

Una forma de medir el tiempo de ejecución de un algoritmo, es tomar el tiempo inicial, luego ejecutar la tarea (en tu caso bubble sort) y luego tomar el tiempo final, haces una resta y puedes obtener aproximadamente el tiempo de ejecución de la tarea.

Puedes usar la función System.currentTimeMillis()

Respecto a escribir archivos, tambien es sencillo y hay varios ejemplos en el foro o en la red, date el tiempo para buscar.

Bueno te dejo un pequeño ejemplo, que mide el tiempo que demora en ordenar un vector de floats aleatorios (10000 posiciones).



Código JAVA:
Ver original
  1. package org.deerme.examples;
  2. /**
  3.  * This code is an example
  4.  * @author deerme.org
  5.  */
  6. public class MedirTiempo {
  7.  
  8.     private float numbers[] = new float[10000];
  9.    
  10.     public MedirTiempo()
  11.     {
  12.         // Llenamos Aleatoriamente el Vector (entre 0 y 100)
  13.         for(int i=0;i<numbers.length;i++)
  14.         {
  15.             numbers[i] = (float) Math.random()*100;
  16.         }        
  17.     }
  18.    
  19.     public void ordenarBubbleSort()
  20.     {
  21.         long time_init,time_total;                
  22.         int i,j,countcicles=0;float aux;
  23.  
  24.         // Medimos el tiempo
  25.         time_init = System.currentTimeMillis();
  26.  
  27.         for (i = 0; i < numbers.length; i++)
  28.         {
  29.             for (j = 0; j < numbers.length - 1; j++)
  30.             {
  31.                 if (numbers[j] > numbers[j+1])
  32.                 {
  33.                     aux = numbers[j];
  34.                     numbers[j] = numbers[j+1];
  35.                     numbers[j+1] = aux;
  36.                     countcicles++;
  37.                 }
  38.             }
  39.         }
  40.         time_total = System.currentTimeMillis() - time_init;
  41.         System.out.println("Nos hemos demorado " +  time_total + " milisegundos en ordenar el vector a travéz de BubbleSort" );
  42.        
  43.     }
  44.  
  45.     public void ImprimirVector()
  46.     {
  47.         for(int i=0;i<numbers.length;i++)
  48.         {
  49.             System.out.println( numbers[i] );
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) {
  54.         MedirTiempo mt = new MedirTiempo();
  55.         mt.ordenarBubbleSort();
  56.  
  57.     }
  58.  
  59. }

Saludos