Foros del Web » Programación para mayores de 30 ;) » Java »

Medir tiempo de Algoritmo

Estas en el tema de Medir tiempo de Algoritmo en el foro de Java en Foros del Web. Necesito tomar el tiempo que toma Aplicar el Algoritmo de BubbleSort en cada uno de los 3 vectores, los cuales uno esta ordenado Aleatoriamente(V1), V2 ...
  #1 (permalink)  
Antiguo 12/10/2011, 12:04
 
Fecha de Ingreso: mayo-2011
Mensajes: 4
Antigüedad: 12 años, 10 meses
Puntos: 0
Pregunta Medir tiempo de Algoritmo

Necesito tomar el tiempo que toma Aplicar el Algoritmo de BubbleSort en cada uno de los 3 vectores, los cuales uno esta ordenado Aleatoriamente(V1), V2 esta Ascendente y V3 Descendente, he imprimir los resultados en un .Txt no tengo idea de si existe una función ya en java para medir el tiempo el milisegundos, y como se escribe exactamente en un txt con java ...

Cita:
public static void main(String[] args) throws IOException
{
int N,j,p,i;
int V1[]= new int [30];
int V2[]= new int [30];
int V3[]= new int [30];

Random rn = new Random();


BufferedReader entrada=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Dame el total de numeros a ordenar: ");
N=Integer.parseInt(entrada.readLine());


for(i=0;i<N;i++)
V1[i]=i+1;
for(j=1; j<=N; j++)
{
do
{
p= rn.nextInt(40);
} while(V1[p] !=0);
V1[p]=j;
}

j=1;
for(i=0; i<N; i++)
{
V2[i]=j;
j++;
}

j=N;
for(i=0; i<N; i++)
{
V3[i]=j;
j--;
}

public static void BubbleSort(int A[], int N)
{
int i,j,aux;

for(i=0; i<N; i++)
for (j=N-1; j>i+1; j--)
{
if(A[j] < A[j-1])
{ aux=A[j];
A[j]=A[j-1];
A[j-1]=aux;
}
}
}

De Antemano Muchas gracias por su ayuda.
  #2 (permalink)  
Antiguo 12/10/2011, 13:02
Avatar de 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
  #3 (permalink)  
Antiguo 12/10/2011, 15:03
 
Fecha de Ingreso: mayo-2011
Mensajes: 4
Antigüedad: 12 años, 10 meses
Puntos: 0
Respuesta: Medir tiempo de Algoritmo

Muchas Gracias DeeR... me ha sido de mucha ayuda :D...

Etiquetas: current, time, timecode
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:16.