Click here to Skip to main content
15,924,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I think the total of milliseconds algorithm is not correct... I need your opinions or the best way to do it

Thanks in advance

What I have tried:

/* ================================ Java ================================== */


Java
public static BigInteger Factorial(int n)
   {
       BigInteger bi = BigInteger.ONE; // Estructura que almacena un numero infinitamente grande y asi se evita el desbordamiento por almacemiento
       for (int i = n; i > 0; i--)
       {
           bi = bi.multiply(BigInteger.valueOf(i));
       }
       return bi;
   }
   public static void main(String[] args)
   {
       // Pedimos la cantidad de veces que queremos correr el ejercicio
       System.out.print("Calculo Factorial --> Ingrese el numero de repeticiones \t");
       Integer NroVeces = Integer.parseInt(new Scanner(System.in).nextLine());
       // Pedimos el numero del cual deseamos calcular el factorial
       System.out.print("Calculo Factorial --> Ingrese el numero \t");
       Integer Nro = Integer.parseInt(new Scanner(System.in).nextLine());

       System.out.println("=================================================");
       for (int i = 0; i < NroVeces; i++)
       {
           // Empezamos a medir el tiempo que llevara la ejecucion de la aplicacion
           long HInicio = System.nanoTime(); // System.currentTimeMillis();
           BigInteger Resultado = Factorial(Nro);  // Invocamos a la funcion que calcula el factorial de un numero cualquiera
           long HFin = System.nanoTime(); // Detenemos la medicion del tiempo que llevo la ejecucion de la aplicacion
           System.out.println(String.format("%d!: %s",Nro, Resultado));  // Mostramos la respuesta

           // ¿¿¿¿ is correct ????
           long Elapsed = HFin - HInicio;
           String ElapsedTime = String.format("%02dh: %02dmin: %02ds: %fms",
                   TimeUnit.NANOSECONDS.toHours(Elapsed),
                   TimeUnit.NANOSECONDS.toMinutes(Elapsed),
                   TimeUnit.NANOSECONDS.toSeconds(Elapsed),
                   TimeUnit.NANOSECONDS.toMillis(Elapsed)%1000.0
           );
           System.out.println("T.Transcurrido: "+ ElapsedTime);
           System.out.println("=================================================\n");
       }

       System.out.print("Presione cualquier tecla para terminar . . . ");
       Scanner input= new Scanner(System.in);
       input.nextLine();
   }
Posted
Updated 8-Dec-16 2:45am
v4

1 solution

The problem is that Windows is a multitasking environment, and there is generally a fair amount going on in the background, which can affect your results. And added to that the speed of modern processors and the .NET JIT compiler, it gets difficult to get a "good" result from a single execution. In order to get "reliable" numbers, you need to time your function for a large number of iterations, and probably run the test several times.
I generally run for maybe 100,000 iterations, but sometimes a smaller number is sufficient. Have a look at this: Counting Lines in a String[^] - it's all about the timings and may give you a clue or two.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900